node-pdfbox is a bridge library to manipulate PDF based on PDFBox (https://pdfbox.apache.org/)
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
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() {
});
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!
});
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();
let numberOfPages = document.pagesCountSync();
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
document.saveSync(); //save current document
document.saveSync('path.pdf') //save as
let page = document.getPageSync(0);
page.getRectSync();
page.getAspectFillScaleSync(100, 100); //width, height
page.getAspectFitScaleSync(100, 100); //width, height
page.getTextSync();
let doc = page.extractSync('/path/to/save');
const scale = 2;
let image = page.getImageSync();
let image1 = page.getImageSync(scale); //scaled
let image2 = page.getImageSync(500, 500); //width, height - crop on center
image.fitSync(100, 100).saveSync('/path/to/save');
image.cropSync(301, 301).saveSync('/path/to/save');
image.saveSync('/path/to/save');
Before install this module, check whether jvm is installed. To compile this module you need to install JDK.
- Working with Encrypting and signing PDFs
- Support to create PDF