Code Monkey home page Code Monkey logo

node-pdftocairo's Introduction

node-pdftocairo

Build Status Codecov npm MIT

Node.js wrapper for pdftocairo - PDF to PNG/JPEG/TIFF/PDF/PS/EPS/SVG using cairo
Inspired by jjwilly16/node-pdftk

Requirements

Since pdftocairo is included in Poppler, you should install Poppler before using this library.

Installation

yarn add node-pdftocairo
import { input } from 'node-pdftocairo';

API

Simple Usage

The first argument of input can be a file path or buffer.
If you pass a output file path to output, it will generate files and returns Promise<null>; otherwise return buffers without generating files.

Generate file buffer(s) from a PDF file

const inputPath = path.join(__dirname, '../test/files/sample.pdf');
const options = { format: 'png' };
const outputBuffer = await input(inputPath, options).output();

Generate file buffer(s) from the buffer

const inputPath = path.join(__dirname, '../test/files/sample.pdf');
const buffer = fs.readFileSync(inputPath);
const options = { format: 'png' };
const outputBuffer = await input(buffer, options).output();

Generate file(s) on the specified output path

const inputPath = path.join(__dirname, '../test/files/sample.pdf');
const outputPath = path.join(__dirname, '../test/files/sample-img');
const options = { format: 'png' };
await input(inputPath, options).output(outputPath);

Options

Reference: Ubuntu Manpage: pdftocairo

Property Description Type Default
bin specify the path of pdftocairo string -
format output file format, should be one of png jpeg tiff ps eps pdf svg string -
antialias Set the cairo antialias option used for text and drawing in image files (or rasterized regions in vector output), should be one of default none gray subpixel fast good best string -
range Specifies the first/last page to convert. { f?: number, l?: number } -
filter Generates only the odd or even numbered pages. string -
singlefile Writes only the first page and does not add digits. boolean false
resolution Specifies the X and Y resolution, in pixels per inch of image files (or rasterized regions in vector output). The default is 150 PPI. number | { x: number, y: number } -
scale Scales the long side of each page (width for landscape pages, height for portrait pages) to fit in scale-to pixels. The size of the short side will be determined by the aspect ratio of the page (PNG/JPEG/TIFF only). number | { x: number, y: number } -
crop Specifies the x-coordinate/y-coordinate of the crop area top left corner in pixels (image output) or points (vector output) and Specifies the width/height/size of crop area in pixels (image output) or points (vector output) (default is 0) { x?: number, y?: number, W?: number, H?: number, sz?: number } -
cropbox Uses the crop box rather than media box when generating the files (PNG/JPEG/TIFF only) boolean false
mono Generate a monochrome file (PNG and TIFF only). boolean false
gray Generate a grayscale file (PNG, JPEG, and TIFF only). boolean false
transparent Use a transparent page color instead of white (PNG and TIFF only). boolean false
level2 Generate Level 2 PostScript (PS only). boolean false
level3 Generate Level 3 PostScript (PS only). This enables all Level 2 features plus shading patterns and masked images. This is the default setting. boolean false
originPageSizes This option is the same as "-paper match". boolean false
icc Use the specified ICC file as the output profile (PNG only). The profile will be embedded in the PNG file. string -
jpegopt When used with -jpeg, takes a list of options to control the jpeg compression. See JPEG OPTIONS for the available options. string -
paper Set the paper size to one of "letter", "legal", "A4", or "A3" (PS,PDF,SVG only). This can also be set to "match", which will set the paper size of each page to match the size specified in the PDF file. If none the -paper, -paperw, or -paperh options are specified the default is to match the paper size. string | { w: number, h: number } -
nocrop By default, printing output is cropped to the CropBox specified in the PDF file. This option disables cropping (PS,PDF,SVG only). boolean false
expand Expand PDF pages smaller than the paper to fill the paper (PS,PDF,SVG only). By default, these pages are not scaled. boolean false
noshrink Don't scale PDF pages which are larger than the paper (PS,PDF,SVG only). By default, pages larger than the paper are shrunk to fit. boolean false
nocenter By default, PDF pages smaller than the paper (after any scaling) are centered on the paper. This option causes them to be aligned to the lower-left corner of the paper instead (PS,PDF,SVG only). boolean false
duplex Adds the %%IncludeFeature: *Duplex DuplexNoTumble DSC comment to the PostScript file (PS only). This tells the print manager to enable duplexing. boolean false
ownerPassword Specify the owner password for the PDF file. Providing this will bypass all security restrictions. string -
userPassword Specify the user password for the PDF file. string -

node-pdftocairo's People

Contributors

codedaraw avatar dependabot-preview[bot] avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

joncv hvjosh sashee

node-pdftocairo's Issues

Why write tmp files?

Is it possible to read directly from stdout and return a buffer instead of writing a tmp file?

async fs

Replace fs.xSync with fs.promises.

Not able to pass bin path

Hello!
I couldn't find much documentation regarding the bin option and I seem to be failing to pass it correctly:
I took the zip from my Lambda Layer with the poppler dependency and extracted it in my project that uses it, inside the integration tests folder. Below you can see the function that uses the input function:

export default async function renderPlan(
  bucket: string,
  key: string,
  size: number
) {
  const pdfBuffer = await fetchBufferFromS3(bucket, key)

  const [imageBuffer] = await input(pdfBuffer, {
    format: 'jpeg',
    scale: size,
    singlefile: true,
    bin: path.join(__dirname, '../../tests/it/opt/bin/pdftocairo'),
  }).output()
  return sharp(imageBuffer, { sequentialRead: true }).trim()
}

I've tried both the relative to the root and to the file path as well as the absolute one, but all give the same error I get when I don't pass any bin:
Screenshot 2024-01-18 at 17 05 47

Could you share more documentation on how the bin path should look like?

Not able to run the code in the aws lambda server

Hi, below is the function I am using to generate a png from the pdf's first-page buffer. it is working fine on my local server but not working on the lambda server. Can we use this library in the lambda server?

 await input(firstPageBuffer, {
   format: "png",
    singlefile: true,
 }).output(firstPagePath);

Thanks.

Need to install tslib package

Hello, there is a issue in your library. You need to install tslib package in your package.json file because without installing these package it is giving the error of Unable to find module 'tsliib'.
Please try to solve it as soon as possible

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.