Split and extract PDF pages

Through this guide, discover how you can create new PDFs by extracting pages from existing documents.

Splitting documents and extracting pages can be achieved with a single tool. You have different options for extracting pages, allowing you to create new documents based on the input file pages.

In your pipeline definition, add a task for the split tool.

Pages extraction methods

Cut a PDF into several parts

This method allows you to cut a PDF at precise positions.

Each cut creates a new output PDF. Each cut creates a new output PDF. So, one cut will result in 2 output files, 2 cuts in 3 files, and so on.

Use the cut parameter to choose this method.

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

      form.append(
        'tasks',
        [
          {
            tool: 'split',
            options: {
              // Cutting the document after the 2nd and 6th pages (3 new documents)
              cuts: [2, 6],
            },
          },
        ],
      );
    
  

Create PDFs from pages

With this method, you will be able to create one or more files, giving the pages you want to include in each.

Use the files parameter to choose this method.

    
      options: {
        files: [
          // Creating one file taking the first and second pages
          { pages: [0, 1] },
          // Creating another file taking the 3rd to 7th pages, plus the last one
          { pages: [{ range: [2, 8] }, 'last'] },
        ],
      }
    
  

Cut a PDF with an interval

This last method allows you to split a PDF every X pages (X being the given interval).

For example, let's take a file of 10 pages, with an interval of 2. This method will allow you to obtain 5 files, each containing 2 pages.

Use the interval parameter to choose this method.

    
      options: {
        // Cut the PDF every 2 pages
        interval: 2,
      }
    
  

Handling multiple input files

When multiple files are processed by a split/extract task, you must ensure that the task options are compatible with all files.

If you know in advance that the options cannot match all the files in the task, you will need to create several tasks, each processing the file(s) compatible with its options.

If the options are not compatible with a file to be processed, an error will be returned and the pipeline will fail.

    
      const form = new FormData();

      // My pipeline treats 2 files
      form.append('file-1', myFirstFile);
      form.append('file-2', mySecondFile);

      form.append('tasks', [
        {
          tool: 'split',
          // Splitting the first file
          files: [0],
          options: {
            // Creating 2 output files
            files: [
              {pages: [0, 10]},
              {pages: [1, 11]},
            ],
          },
        },
        {
          tool: 'split',
          // Splitting the second file
          files: [1],
          options: {
            // Cut the PDF at the middle
            cuts: ['last/2'],
          },
        },
      ]);