Images in PDFKit

Adding images to PDFKit documents is an easy task. Just pass an image path, buffer, or data uri with base64 encoded data tothe image method along with some optional arguments. PDFKit supports theJPEG and PNG formats. If an X and Y position are not provided, the image isrendered at the current point in the text flow (below the last line of text).Otherwise, it is positioned absolutely at the specified point. The image willbe scaled according to the following options.

  • Neither width or height provided - image is rendered at full size
  • width provided but not height - image is scaled proportionally to fit in the provided width
  • height provided but not width - image is scaled proportionally to fit in the provided height
  • Both width and height provided - image is stretched to the dimensions provided
  • scale factor provided - image is scaled proportionally by the provided scale factor
  • fit array provided - image is scaled proportionally to fit within the passed width and height
    Here is an example showing some of these options.
  1. # Scale proprotionally to the specified width
  2. doc.image('images/test.jpeg', 0, 15, width: 300)
  3. .text('Proportional to width', 0, 0)
  4. # Fit the image within the dimensions
  5. doc.image('images/test.jpeg', 320, 15, fit: [100, 100])
  6. .rect(320, 15, 100, 100)
  7. .stroke()
  8. .text('Fit', 320, 0)
  9. # Stretch the image
  10. doc.image('images/test.jpeg', 320, 145, width: 200, height: 100)
  11. .text('Stretch', 320, 130)
  12. # Scale the image
  13. doc.image('images/test.jpeg', 320, 280, scale: 0.25)
  14. .text('Scale', 320, 265)

This example produces the following output:

Images  - 图1

That is all there is to adding images to your PDF documents with PDFKit. Nowlet's look at adding annotations.

PreviousNext

原文: http://pdfkit.org/docs/images.html