Images to PDF

Use the Visual PDF API to turn images (JPG, PNG) into PDF.

Basic images to PDF transformation

The images to PDF transformation tool is accessible in a pipeline using a images-to-pdf task.

    
      // Creating the form data for the pipeline
      const form = new FormData();

      // Adding 2 images to the pipeline
      form.append('image-1', myFirstImage);
      form.append('image-2', mySecondImage);

      // Turning the images in a PDF
      form.append('tasks', [{ tool: 'images-to-pdf' }]);
    
  

Custom page size

By default, the pages of the generated PDF will have the size of the images.

You can however change this behavior by using the page-size option and choosing between one of the following values:

  • fit: this is the default value. With this option, the pages will have the size of their image
  • A4: with this option, the images will each be integrated into an A4 page
    
      form.append(
        'tasks',
        [
          {
            tool: 'images-to-pdf',
            // Inserting images into A4 pages
            options: { 'page-size': 'A4' },
          },
        ],
      );
    
  

Custom page margins

You can add a margin around your images using the margins option.

You can set a common margin for all sides (with a single value), or a custom margin for each side (with an array in the format [left, top, right, bottom]).

    
      // Generating one PDF per image
      options: {
        'page-size': 'A4',
        // Adding a margin of 10 to the left and right, and a margin of 20 to the top and bottom
        margins: [10, 20, 10, 20],
      }
    
  

Alternatively, you can use relative units to calculate margins based on image dimensions

    
      // Generating one PDF per image
      options: {
        'page-size': 'A4',
        // Adding a margin of 10% of the image width
        margins: '10w',
      }
    
  

Get one file per image

If you want a PDF to be generated for each input image (instead of a single PDF containing multiple images), you can change the split option.

    
      // Generating one PDF per image
      options: { split: true }