This tutorial describes how to extract pages from a PDF document from the command line. There are online tools available for splitting PDFs but if you prefer not to share your PDF files with a third-party, you can split them into separate pages easily from the command line.

Split PDF Files

Assuming that you have node installed on your computer, run the following command in the terminal to initialize the environment:

$ mkdir pdf-split
$ cd pdf-split
$ npm init -y

Next, we’ll install the popular pdf-lib package from the npm registry. PDF library is written in TypeScript and it is a very tool for creating and manipulating PDF files. You can learn more about PDF library at js.org.

In addition to splitting PDF files, the PDF library can also be used for merging multiple PDF files into a single PDF file. Or for rearranging the pages of a PDF file.

$ npm install --save pdf-lib

Next, we’ll write a simple Node.js script that splits a PDF file into multiple PDF files. You need to provide the path of the input PDF file and the output folder.

// split.pdf.js
const fs = require('fs');
const path = require('path');
const { PDFDocument } = require('pdf-lib');

const splitPDF = async (pdfFilePath, outputDirectory) => {
  const data = await fs.promises.readFile(pdfFilePath);
  const readPdf = await PDFDocument.load(data);
  const { length } = readPdf.getPages();

  for (let i = 0, n = length; i < n; i += 1) {
    const writePdf = await PDFDocument.create();
    const [page] = await writePdf.copyPages(readPdf, [i]);
    writePdf.addPage(page);
    const bytes = await writePdf.save();
    const outputPath = path.join(outputDirectory, `Invoice_Page_${i + 1}.pdf`);
    await fs.promises.writeFile(outputPath, bytes);
    console.log(`Added ${outputPath}`);
  }
};

splitPDF('input/invoices.pdf', 'invoices').then(() =>
  console.log('All invoices have been split!').catch(console.error)
);

In the above example, we have a large PDF file that contains multiple invoices generated from the Tally accounting system. We want to split the PDF file into multiple PDF files such that each invoice is a separate PDF file.

You can run the above script in the terminal to split the PDF file.

$ node split.pdf.js

Compress Large PDF files

The one downside of this approach is that the generated PDF files are large in size. You can however use the ghostscript command line utility to highly compress the size of split PDF files.

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.2 -r200 -dPDFSETTINGS=/screen -dEmbedAllFonts=true -dSubsetFonts=true -dPrinted=false -dNOPAUSE -dQUIET -dBATCH -sOutputFile=c12_{filename} {filename}

Also see: Useful FFMPEG Commands



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *

Author

prakhar@affmantra.com

Related Posts

How to Handle OAuth Permissions in Google Add-ons

Table of Contents 1. How to Check for Required OAuth Scopes 1.1 The “Authorization Catch-22” Problem 1.2 How to Reset the Permissions...

Read out all

How to Recover Permanently Deleted Files and Folders in Google Drive

Table of Contents When you delete any file or folder in your Google Drive, it is moved to the trash folder. The...

Read out all

Simple URL Tricks for Google Drive You Should Know

Table of Contents 1. Google Drive URL Tricks 1.1 Google Drive Web Viewer 1.2 Reader Mode for Google Drive Files 1.3 Embed...

Read out all

How to Extract URLs from HYPERLINK Function in Google Sheets

The HYPERLINK formula of Google Sheets lets you insert hyperlinks into your spreadsheets. The function takes two arguments: The full URL of...

Read out all

Find and Remove Inactive Users in your Google Workspace Domain

Table of Contents 1. Find the inactive users in Google Workspace domain You can use Google Apps Script to find all the...

Read out all

The Best Online Tools To Know Everything About a Website

The Best Online Tools To Know Everything About a Website Source link

Read out all