PDFKit is a Node.js library that allows you to create PDF files easily. It provides an API for creating text, graphics, and other content, which can be added to a PDF document. One of the most common use cases for PDFKit is to generate reports, invoices, or other documents that contain a mix of text and images.
When working with PDFKit, one important concept to understand is the concept of nodes. Nodes are the building blocks of PDF documents, and they represent different types of content, such as text, images, or shapes. Each node has a position, a size, and other properties that determine how it will be displayed in the document.
In this blog post, we will focus on how to add an image from a URL to a PDF document using PDFKit and the downloadImage
function provided in the code snippet.
Downloading an Image from a URL
Before we can add an image to a PDF document, we first need to download the image from a URL. The downloadImage
function provided in the code snippet can be used to download an image from a URL and convert it to a Buffer object. This function uses the Axios library to perform the HTTP request and set the response type to arraybuffer
.
Here's how you can use the downloadImage
function to download an image from a URL:
const downloadImage = async (url) => { return axios({ url, method: 'GET', responseType: 'arraybuffer', }).then((response) => Buffer.from(response.data)); };
This will download the image at the specified URL and store it in a Buffer object.
Adding an Image to a PDF Document
Once we have the image as a Buffer object, we can add it to a PDF document using PDFKit. Here's how you can create a new PDF document and add an image to it:
const PDFDocument = require('pdfkit'); const fs = require('fs'); const doc = new PDFDocument(); const imageBuffer = await downloadImage('https://example.com/image.jpg'); doc.image(imageBuffer, { fit: [250, 250], // set the size of the image }); doc.pipe(fs.createWriteStream('output.pdf')); doc.end();
In this example, we create a new PDF document using the PDFDocument class from PDFKit. We then download the image from a URL using the downloadImage
function and store it in a Buffer object.
To add the image to the PDF document, we call the image
method on the PDFDocument object and pass in the image buffer as the first argument. We also pass in an options object that sets the size of the image to 250x250 pixels.
Finally, we pipe the PDF document to a file stream using the pipe
method and call the end
method to finish creating the document.
Conclusion
In this blog post, i have shown you how to add an image from a URL to a PDF document using PDFKit and the downloadImage
function provided in the code snippet. i have also explained the concept of nodes in PDFKit and how they are used to represent different types of content in a PDF document.
PDFKit is a powerful library that provides a lot of flexibility when it comes to creating PDF documents. Whether you're generating reports, invoices, or other types of documents, PDFKit can help you create professional-looking documents with ease.
Learn more about PDFKit github.