Skip to content

Commit 63654ca

Browse files
author
Andrey
committed
Added Watermark API
1 parent 9f4ac2a commit 63654ca

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed
0 Bytes
Binary file not shown.
320 KB
Binary file not shown.

index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,46 @@ app.get('/replaceContent/:name', (req, res) => {
207207
PDFNetEndpoint(main, outputPath, res);
208208
});
209209

210+
app.get('/watermark/:filename-:watermark', (req, res) => {
211+
const filename = req.params.filename;
212+
const watermark = req.params.watermark;
213+
let ext = path.parse(filename).ext;
214+
215+
if (ext !== '.pdf') {
216+
res.statusCode = 500;
217+
res.end(`File is not a PDF. Please convert it first.`);
218+
}
219+
220+
const inputPath = path.resolve(__dirname, filesPath, filename);
221+
const outputPath = path.resolve(__dirname, filesPath, `${filename}_watermarked.pdf`);
222+
223+
const main = async () => {
224+
const pdfdoc = await PDFNet.PDFDoc.createFromFilePath(inputPath);
225+
await pdfdoc.initSecurityHandler();
226+
227+
const stamper = await PDFNet.Stamper.create(
228+
PDFNet.Stamper.SizeType.e_relative_scale,
229+
0.5,
230+
0.5,
231+
); // Stamp size is relative to the size of the crop box of the destination page
232+
stamper.setAlignment(
233+
PDFNet.Stamper.HorizontalAlignment.e_horizontal_center,
234+
PDFNet.Stamper.VerticalAlignment.e_vertical_center,
235+
);
236+
const redColorPt = await PDFNet.ColorPt.init(1, 0, 0);
237+
stamper.setFontColor(redColorPt);
238+
const pgSet = await PDFNet.PageSet.createRange(1, await pdfdoc.getPageCount());
239+
stamper.stampText(pdfdoc, watermark, pgSet);
240+
241+
pdfdoc.save(
242+
outputPath,
243+
PDFNet.SDFDoc.SaveOptions.e_linearized,
244+
);
245+
};
246+
247+
PDFNetEndpoint(main, outputPath, res);
248+
});
249+
210250
const PDFNetEndpoint = (main, pathname, res) => {
211251
PDFNet.runWithCleanup(main)
212252
.then(() => {

0 commit comments

Comments
 (0)