Skip to content

Instantly share code, notes, and snippets.

@danawoodman
Created April 10, 2015 19:41
Show Gist options
  • 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();
@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