Skip to content

Commit 9f4ac2a

Browse files
author
Andrey
committed
Added replace content API
1 parent 97edda9 commit 9f4ac2a

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

files/template_letter.pdf

18.7 KB
Binary file not shown.
33.2 KB
Binary file not shown.

index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,33 @@ app.get('/textextract/:filename-:pagenumber', (req, res) => {
180180
PDFNetEndpoint(main, outputPath, res);
181181
});
182182

183+
app.get('/replaceContent/:name', (req, res) => {
184+
const name = req.params.name.replace('_', ' ');
185+
const filename = 'template_letter.pdf'
186+
187+
const inputPath = path.resolve(__dirname, filesPath, filename);
188+
const outputPath = path.resolve(__dirname, filesPath, `${filename}_replaced.pdf`);
189+
190+
const main = async () => {
191+
const pdfdoc = await PDFNet.PDFDoc.createFromFilePath(inputPath);
192+
await pdfdoc.initSecurityHandler();
193+
const replacer = await PDFNet.ContentReplacer.create();
194+
const page = await pdfdoc.getPage(1);
195+
196+
await replacer.addString('NAME', name);
197+
await replacer.addString('Address', '123 Main St, Vancouver, BC CANADA');
198+
await replacer.addString('DATE', new Date(Date.now()).toLocaleString());
199+
await replacer.process(page);
200+
201+
pdfdoc.save(
202+
outputPath,
203+
PDFNet.SDFDoc.SaveOptions.e_linearized,
204+
);
205+
};
206+
207+
PDFNetEndpoint(main, outputPath, res);
208+
});
209+
183210
const PDFNetEndpoint = (main, pathname, res) => {
184211
PDFNet.runWithCleanup(main)
185212
.then(() => {

0 commit comments

Comments
 (0)