I'm looking for a solution that will allow me to overlay text generated from a variable onto a PDF, where the text has a white fill and a black outline. Does anyone know of a sensible solution to this problem? Of course, it can't be any of the Adobe programs like Illustrator, etc.
I've written a script in Node.js using the PDFKit library. However, it's not an ideal solution, since this library always adds an extra page with the text at the end, which then has to be merged onto the production PDF separately.
Does anyone have any interesting ideas?
I'm attaching the script I wrote:
import * as fs from "fs";
import * as tmp from "tmp";
import * as path from "path";
import PDFDocument from "pdfkit";
async function jobArrived(s: Switch, flowElement: FlowElement, job: Job) {
await job.log(LogLevel.Info, "Starting PDF generation with PDFKit + custom font");
// Get the text to insert
const highlightText = await job.getPrivateData("text") || "No text provided";
await job.log(LogLevel.Info, "Text to insert: '%1'", [highlightText]);
// Path to the TTF font (make sure the file is next to this script)
const fontPath = __dirname + "/Library/Fonts/Futura Md BT Medium.ttf";
if (!fs.existsSync(fontPath)) {
await job.log(LogLevel.Error, "TTF font file not found: %1", [fontPath]);
await job.sendToData(Connection.Level.Error);
return;
}
await job.log(LogLevel.Info, "TTF font found: %1", [fontPath]);
// Create a temporary file for the resulting PDF
const tmpFile = tmp.fileSync({ postfix: ".pdf" });
// Create a new A4 PDF document
const doc = new PDFDocument({ size: "A4" });
const fontSize = 36;
const x = 20;
const y = 842 - 20 - fontSize; // Bottom left corner, PDFKit: (0,0) is top LEFT
// Open a stream to the temporary file
const stream = fs.createWriteStream(tmpFile.name);
doc.pipe(stream);
// Set font and text style
doc.font(fontPath)
.fontSize(fontSize)
.fillColor("white")
.strokeColor("black")
.lineWidth(1.5)
.text(highlightText, x, y, {
stroke: true,
fill: true
});
doc.end();
await job.log(LogLevel.Info, "PDFKit finished writing PDF: %1", [tmpFile.name]);
await new Promise(r => setTimeout(r, 5000)); // 5 seconds wait!
await job.log(LogLevel.Info, "Attempting to send to sendToData after 5 seconds");
await job.setPrivateData("pdfTempPath", tmpFile.name);
await job.sendToData(Connection.Level.Success, tmpFile.name);
}
Stroke on text in pdf
- JimmyHartington
- Advanced member
- Posts: 460
- Joined: Tue Mar 22, 2011 7:38 am
Re: Stroke on text in pdf
Just to be sure. You do not have Pitstop Server available to you?