npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pdf-flattening

v1.0.2

Published

A nodejs module for flattening a PDF file

Downloads

7

Readme

pdf-flattening

pdf-flattening is a Node.js library that provides an easy-to-use interface for converting PDF files into images and then recompiling them back into a single PDF document. This process is often referred to as "flattening" and can be particularly useful for simplifying the structure of PDF documents, making them easier to handle in various applications.

Installation

To install pdf-flattening, run the following command in your project directory:

npm install pdf-flattening

Make sure you have GraphicsMagick and Ghostscript installed on your system, as they are required for PDF processing:

GraphicsMagick: http://www.graphicsmagick.org/ Ghostscript: https://www.ghostscript.com/

Usage

Flatten PDF and Return as Buffer

To flatten a PDF and get the result as a buffer (useful for immediate processing or sending over the web):

const fs = require('fs');
const flattener = require('pdf-flattening');

// Read your PDF file into a buffer
const pdfBuffer = fs.readFileSync('path/to/your/document.pdf');

// Flatten the PDF and get the result as a buffer
flattener.flatten(pdfBuffer, { saveToFile: false })
  .then(resultBuffer => {
    // Do something with the resultBuffer, like saving it to a file or sending it in a response
    fs.writeFileSync('path/to/your/flattened.pdf', resultBuffer);
  })
  .catch(err => {
    console.error('An error occurred:', err);
  });

Flatten PDF and Save to File

To flatten a PDF and save the flattened version directly to a file:

const fs = require('fs');
const flattener = require('pdf-flattening');

// Read your PDF file into a buffer
const pdfBuffer = fs.readFileSync('path/to/your/document.pdf');

// Specify the output path and saveToFile option
flattener.flatten(pdfBuffer, { path: 'output/path', saveToFile: true })
  .then(outputPath => {
    console.log(`Flattened PDF saved to: ${outputPath}`);
  })
  .catch(err => {
    console.error('An error occurred:', err);
  });

PDF to Image Conversion

To convert a PDF page to an image using the pdf2img functionality within pdf-flattening:


const Pdf2Img = require('pdf-flattening/pdf2img');
const pdf2img = new Pdf2Img();

pdf2img.setOptions({
  type: 'jpg', // or 'png'
  density: 300,
  outputdir: 'path/to/output/images',
  outputname: 'myImage'
});

pdf2img.convert('path/to/your/document.pdf')
  .then(results => {
    console.log('Images:', results);
  })
  .catch(err => {
    console.error('Conversion error:', err);
  });

API Reference

Pdf2Img

setOptions(options)

Set conversion options.

Options The flatten method accepts an options object which allows you to customize the behavior of the PDF flattening process. Here are the available options:

  • path: Specifies the directory path where temporary files will be stored during processing. Defaults to the current directory.
  • type: Determines the image format to use for the intermediate conversion. Can be 'jpg' or 'png'. Default is 'jpg'.
  • density: Sets the DPI (dots per inch) for the intermediate images. A higher value results in better quality but larger files. Default is 200.
  • saveToFile: A boolean that specifies whether the flattened PDF should be saved to a file or returned as a buffer. If true, the method returns the path to the saved file. If false, it returns a buffer. Default is false.

convert(input)

Convert a PDF file to images.

  • input: Path to the PDF file to convert.
  • Returns a Promise that resolves with the conversion results or rejects with an error.

Flattener

flatten(buffer, options)

Flatten a PDF document.

  • buffer: A buffer containing the PDF data to flatten.
  • options: An object containing options such as the path for temporary files, image type, and density.
  • Returns a Promise that resolves with a buffer containing the flattened PDF or rejects with an error.

Contributing

Contributions are welcome! Please submit all pull requests to the repository.

License

pdf-flattening is MIT licensed.