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-extractor

v2.2.0

Published

Node.js module for rendering pdf pages to images, svgs, html files, text files and json metadata.

Downloads

1,231

Readme

pdf-extractor

Pdf-extractor is a wrapper around pdf.js to generate images, svgs, html files, text files and json files from a pdf on node.js.

  • Image: A DOM Canvas is used to render and export the graphical layer of the pdf. Canvas exports *.png as a default but can be extended to export to other file types like *.jpg.
  • SVG: Pdf objects are converted to svg using the SVGGraphics parser of pdf.js.
  • HTML: Pdf text is converted to HTML. This can be used as a (transparent) layer over the image to enable text selection.
  • Text: Pdf text is extracted to a text file for different usages (e.g. indexing the text).

PDF.js on Node.js

This library is in it's most basic form a node.js wrapper for pdf.js. It has default renderers to generate a default output, but is easily extended to incorporate custom logic or to generate different output. It uses a node.js DOM and the node domstub from pdf.js do make pdf parsing available on node.js without a browser.

Box View / Crocodoc

This project is inspired by the Box View / Crocodoc way of converting documents (with this tool pdfs) to web-assets: images and html. The generated files match the files of Box View. This makes this library an option to transition from the Box View API to an open-source solution.

Examples

This library can be used as-is to generate assets from a pdf. The only requirements are a pdf as input and a writable directory as output. The extractor can also be used for rendering in different ways. The renderers can be extended or new ones can be injected into the extractor to render a pdf in new ways.

Extractor

How to use the default extractor to render png, html and text files for pdf pages:

const PdfExtractor = require('pdf-extractor').PdfExtractor;

let outputDir = '/path/to/output',

pdfExtractor = new PdfExtractor(outputDir, {
    viewportScale: (width, height) => {
        //dynamic zoom based on rendering a page to a fixed page size 
        if (width > height) {
            //landscape: 1100px wide
            return 1100 / width;
        }
        //portrait: 800px wide
        return 800 / width;
    },
    pageRange: [1,5],
});

pdfExtractor.parse('/path/to/dummy.pdf').then(function () {
	console.log('# End of Document');
}).catch(function (err) {
	console.error('Error: ' + err);
});

This results in these generated files:

info.json
page-1.png
page-2.png
page-3.png
page-4.png
page-5.png
stylesheet.css
text-1.html
text-1.txt
text-2.html
text-2.txt
text-3.html
text-3.txt
text-4.html
text-4.txt
text-5.html
text-5.txt

Using a custom renderer

It is relatively easy to extend or create new renderers/writers. Below an example to use the Canvas capability for *.jpg file generation.

const PdfExtractor = require('pdf-extractor').PdfExtractor;
const CanvasRenderer = require('pdf-extractor').CanvasRenderer;
const SvgRenderer = require('pdf-extractor').SvgRenderer;
const FileWriter = require('pdf-extractor').FileWriter;

class JPGWriter extends FileWriter
{
	getFilePathForPage(page) {
		return super.getPagePath(page.pageNumber, 'png');
	}

	writeCanvasPage(page, viewport, canvas) {
		return this.writeStreamToFile(canvas.jpgStream(), this.getFilePathForPage(page))
	}
}

class JPGCanvasRenderer extends CanvasRenderer
{
	getWriters(writerOptions) {
		let writers = super.getWriters(writerOptions);
		writers.push(new JPGWriter(this.outputDir, writerOptions));
		return writers;
	}
}

let outputDir = '/path/to/output',

pdfExtractor = new PdfExtractor(outputDir, {
	renderers: [
		new JPGCanvasRenderer(outputDir, rendererOptions),
		new SvgRenderer(outputDir, rendererOptions)
	]
});

pdfExtractor.parse('/path/to/dummy.pdf').then(function () {
	console.log('# End of Document');
}).catch(function (err) {
	console.error('Error: ' + err);
});

This adds jpg images to the generated files.