pdf-create.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // Create a PDF from scratch using helper functions.
  2. // This example creates a new PDF file from scratch, using helper
  3. // functions to create resources and page objects.
  4. // This assumes a basic working knowledge of the PDF file format.
  5. // Create a new empty document with no pages.
  6. var pdf = new PDFDocument()
  7. // Load built-in font and create WinAnsi encoded simple font resource.
  8. var font = pdf.addSimpleFont(new Font("Times-Roman"))
  9. // Load PNG file and create image resource.
  10. var image = pdf.addImage(new Image("example.png"))
  11. // Create resource dictionary.
  12. var resources = pdf.addObject({
  13. Font: { Tm: font },
  14. XObject: { Im0: image },
  15. })
  16. // Create content stream data.
  17. var contents =
  18. "10 10 280 330 re s\n" +
  19. "q 200 0 0 200 50 100 cm /Im0 Do Q\n" +
  20. "BT /Tm 16 Tf 50 50 TD (Hello, world!) Tj ET\n"
  21. // Create a new page object.
  22. var page = pdf.addPage([0,0,300,350], 0, resources, contents)
  23. // Insert page object at the end of the document.
  24. pdf.insertPage(-1, page)
  25. // Save the document to file.
  26. pdf.save("out.pdf", "pretty,ascii,compress-images,compress-fonts")