Notorious Preying Mantis

    node-pdfbox

    0.1.9 • Public • Published

    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

    Install

    npm i node-pdfbox

    DownloadsWeekly Downloads

    24

    Version

    0.1.9

    License

    MIT

    Last publish

    Collaborators

    • pedro-lucas