Skip to content

Instantly share code, notes, and snippets.

@danawoodman
Created April 10, 2015 19:41
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save danawoodman/ae72b838b7aa56869082 to your computer and use it in GitHub Desktop.
Save danawoodman/ae72b838b7aa56869082 to your computer and use it in GitHub Desktop.
Example of using pdfkit in Node.js
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();
@sm-programmer
Copy link

Nicely done! I like it! :D

@mjschuetze
Copy link

Thank you

@josh-authy
Copy link

Simple and straight forward. Thanks.

@Nordalf
Copy link

Nordalf commented Aug 16, 2017

What if I have this in a post route? Do I need an emitter?

@Ed-Escubedo
Copy link

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?

@amritendu-cbnits
Copy link

I am having the same issue any solution please

@doguinho
Copy link

doguinho commented Feb 7, 2020

Thank you! Official DOC has a not working example.

@danawoodman
Copy link
Author

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

@bartvandoormaal
Copy link

The issue of the permissions not working is because the ownerPassword property is missing. I needs to be set for the permissions to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment