Coder Social home page Coder Social logo

node-pdfbox's Introduction

Node PDFBox

node-pdfbox is a bridge library to manipulate PDF based on PDFBox (https://pdfbox.apache.org/)

Installation

Dependencies

This module require JVM configured on host machine to work. When you use this module with nw.js you need to specify this requisite on installation process.

You can use node-pdfbox with NW.js, but don't forget to install JDK to compile java module, after that you can distribute you app in a machine with JVM installed.

npm install --save node-pdfbox

Quick Examples

If you want to call async operations just use the method without Sync suffix.

Sync

  page.extractSync('path.pdf');

Async

  page.extract('path.pdf')
  .then(function() {

  })
  .catch(function(err) {

  })
  .finally(function() {

  });

Basic operations

let Document = require('node-pdfbox');

let document = Document.loadSync('/path/to/document.pdf');
let page = document.getPageSync(0);
let image = page.getImageSync();
let imageScaled = page.getImageSync(2.0);
let imageScaledToFill = page.getImageSync(1300, 1800);

let newDocument = page.extractSync('path-to-new-document.pdf');
newDocument.addPageSync(document.getPageSync(3));
newDocument.addPagesSync(document, 0, 10, 1);

image.fitSync(100, 100).saveSync('path.jpg');
image.cropSync(100, 100).saveSync('path.jpg');
image.cropSync(0, 0, 100, 100).saveSync('path.jpg');
image.cropSync(100, 100, {
  vertical: 'bottom',
  horizontal: 'right'
}).saveSync('path.jpg');

image.saveSync('path.jpeg', 'jpg');

or

Document.load('/path/to/document.pdf')
.then(function(document) {
  return document.getPage(0);
})
.then(function(page) {
  return page.getImage(2.0) //scale
})
.then(function(image) {
  //do something ...
})
.catch(function(err) {
  console.error(err);
})
.finally(function() {
  //Done!
});

Get information

let title = document.getInfoSync('Title');
let title1 = document.getTitleSync(); //The same of getInfo
let author = document.getAuthorSync();
let subject = document.getSubjectSync();
let keywords = document.getKeywordsSync();

Number of pages

let numberOfPages = document.pagesCountSync();

Add pages to document

document.addPageSync(page); //PDFPage to end of file
document.addPageSync('path.pdf', 0, 0); // add page (second) from path (first) at index (third)  

document.addPagesSync('path.pdf'); //Copy all pages from path to end of document
document.addPagesSync(otherDocument); //Copy all pages from PDFDocument
document.addPagesSync(otherDocument, 0); //atIndex
document.addPagesSync(otherDocument, 0, 10, 0); //start, end, atIndex

Save document

document.saveSync(); //save current document
document.saveSync('path.pdf') //save as

Get page

let page = document.getPageSync(0);

Get crop box rect

page.getRectSync();

Get scale to fill

page.getAspectFillScaleSync(100, 100); //width, height

Get scale to fit

page.getAspectFitScaleSync(100, 100); //width, height

Get text

page.getTextSync();

Extract page

let doc = page.extractSync('/path/to/save');

Create image

const scale = 2;

let image = page.getImageSync();
let image1 = page.getImageSync(scale); //scaled
let image2 = page.getImageSync(500, 500); //width, height - crop on center

Fit image

image.fitSync(100, 100).saveSync('/path/to/save');

Crop image

image.cropSync(301, 301).saveSync('/path/to/save');

Save image

image.saveSync('/path/to/save');

Known issues

Before install this module, check whether jvm is installed. To compile this module you need to install JDK.

Not implemented yet

  • Working with Encrypting and signing PDFs
  • Support to create PDF

node-pdfbox's People

Contributors

pedro-lucas avatar chaurasiaravi avatar vaibhavbhuva avatar cksachdev avatar srivathsa-nagaraj avatar

Watchers

James Cloos avatar

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.