-
-
Save danawoodman/ae72b838b7aa56869082 to your computer and use it in GitHub Desktop.
var fs = require('fs'); | |
var PDFDocument = require('pdfkit'); | |
var pdf = new PDFDocument({ | |
size: 'LEGAL', // See other page sizes here: https://github.com/devongovett/pdfkit/blob/d95b826475dd325fb29ef007a9c1bf7a527e9808/lib/page.coffee#L69 | |
info: { | |
Title: 'Tile of File Here', | |
Author: 'Some Author', | |
} | |
}); | |
// Write stuff into PDF | |
pdf.text('Hello World'); | |
// Stream contents to a file | |
pdf.pipe( | |
fs.createWriteStream('./path/to/file.pdf') | |
) | |
.on('finish', function () { | |
console.log('PDF closed'); | |
}); | |
// Close PDF and write file. | |
pdf.end(); |
Thank you
Simple and straight forward. Thanks.
What if I have this in a post route? Do I need an emitter?
I am using PDFkit to prepare a certificate document and it is working well...(in response to AlexanderFalk, I am preparing the document in a post route using: response.contentType("application/pdf"); doc.pipe(res); after creating the document).
I am having trouble blocking printing, modification, etc in the generated document. The document is being generated in the post route and sent for viewing at the browser. The intent is that the document just be viewed, but a user could download it and save it. When the downloaded document is viewed on a Mac (Adobe Acrobat Reader DC v2019.010.20098), it can be printed and annotated even though I tried to preclude that when the document was generated on the server. I could not find an example that used the permissions object, but reading the documentation, I used the following when creating the PDFDocument:
let doc = new PDFDocument({
size: 'LETTER',
info:{Title:'someTitle', Author:'someAuthor'},
permissions:{
printing:false,
modifying:false,
copying:false,
annotating:false,
fillingForms:false,
contentAccessibility:false,
documentAssembly:false
}
});
res.contentType("application/pdf");
doc.pipe(res);
...
doc.end();
Is there anything I could do to limit the ability to modify or print the document if it is downloaded by a user?
I am having the same issue any solution please
Thank you! Official DOC has a not working example.
Didn't notice all the comments till now. Glad it is helping others!
Regarding using with Express, maybe try streaming the response instead of writing to the file?
https://stackoverflow.com/questions/38788721/how-do-i-stream-response-in-express
The issue of the permissions not working is because the ownerPassword property is missing. I needs to be set for the permissions to work.
Nicely done! I like it! :D