Skip to content

Commit

Permalink
#28 #26 Fix overlay coordinate and add options to overlay pdf.
Browse files Browse the repository at this point in the history
  • Loading branch information
chunyenHuang committed Feb 14, 2018
1 parent b16a729 commit 95218aa
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 8 deletions.
12 changes: 8 additions & 4 deletions lib/Recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ class Recipe {
return { x: ox, y: oy };
}

read(src) {
read(inSrc) {
const isForExternal = (inSrc) ? true : false;
try {
src = src || this.src;
const src = (isForExternal) ? inSrc : this.src;
const pdfReader = hummus.createReader(src);
this.pdfReader = pdfReader;
const pages = pdfReader.getPagesCount();
if (pages == 0) {
// broken or modify password protected
Expand Down Expand Up @@ -152,7 +152,11 @@ class Recipe {
};
metadata[page.pageNumber] = page;
}
this.metadata = metadata;
if (!isForExternal) {
this.pdfReader = pdfReader;
this.metadata = metadata;
}
return metadata;
} catch (err) {
throw new Error(err);
};
Expand Down
62 changes: 59 additions & 3 deletions lib/overlay.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const xObjectForm = require('./xObjectForm');

/**
* Overlay a pdf to the current pdf
* @name overlay
Expand All @@ -6,10 +8,64 @@
* @param {string} pdfSrc - The path for the overlay pdf
* @param {number} x - The coordinate x
* @param {number} y - The coordinate y
* @param {number} [options.scale] - Scale the overlay pdf, default is 1
* @param {boolean} [options.keepAspectRatio] - To keep the aspect ratio when scaling, default is true
* @param {boolean} [options.fitWidth] - To set the width to 100% (use with keepAspectRatio=true)
* @param {boolean} [options.fitHeight] - To set the height to 100% (use with keepAspectRatio=true)
*/
exports.overlay = function overlay(pdfSrc, x, y, options = {}) {
const { nx, ny } = this._calibrateCoorinate(x, y);
exports.overlay = function overlay(pdfSrc, x = 0, y = 0, options = {}) {
// allow to have only 2 arguments input
if (arguments.length == 2) {
options = x || {};
x = 0;
y = 0;
}
const pathOptions = this._getPathOptions(options);
const gsId = this._getPathOptions(options).fillGsId;

const { keepAspectRatio, fitWidth, fitHeight } = options;
const scale = options.scale || 1;

const { width: pageWidth, height: pageHeight } = this.metadata[this.pageNumber];

const inMetadata = this.read(pdfSrc);
// for now only handle the first page.
const { width, height } = inMetadata[1];

this.pauseContext();
const xObject = new xObjectForm(this.writer, width, height);
xObject.getContentContext()
.q()
.drawImage(0, 0, pdfSrc)
.Q();
xObject.end();
this.resumeContext();

const context = this.pageContext;
context.drawImage(nx, ny, pdfSrc);
let scaleX = 1 * scale;
let scaleY = 1 * scale;

if (fitWidth) {
scaleX = pageWidth / width;
if (keepAspectRatio) {
scaleY = scaleX;
}
}
if (fitHeight) {
scaleY = pageHeight / height;
if (keepAspectRatio) {
scaleX = scaleY;
}
}

const posX = x;
const posY = pageHeight - height * scaleY - y;

context
.q()
.cm(scaleX, 0, 0, scaleY, posX, posY)
.doXObject(xObject)
.Q();

return this;
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added tests/materials/issue-28-input.pdf
Binary file not shown.
Binary file modified tests/materials/test-info.pdf
Binary file not shown.
Binary file modified tests/materials/test.pdf
Binary file not shown.
82 changes: 82 additions & 0 deletions tests/overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,86 @@ describe('Modify', () => {
.endPage()
.endPDF(done);
});

it('Add Overlay from other PDF - position', (done) => {
const src = path.join(__dirname, 'materials/issue-28-input.pdf')
const overlayPDF = path.join(__dirname, 'materials/test2.pdf');
const output = path.join(__dirname, `output/Add overlay (#28) - position.pdf`);

const recipe = new HummusRecipe(src, output);
const options = {}
recipe
.editPage(1)
.overlay(overlayPDF, 500, 300, options)
.endPage()
.endPDF(done);
});

it('Add Overlay from other PDF - scale', (done) => {
const src = path.join(__dirname, 'materials/issue-28-input.pdf')
const overlayPDF = path.join(__dirname, 'materials/test2.pdf');
const output = path.join(__dirname, `output/Add overlay (#28) - scale.pdf`);

const recipe = new HummusRecipe(src, output);
const options = {
keepAspectRatio: true,
scale: 3
}
recipe
.editPage(1)
.overlay(overlayPDF, options)
.endPage()
.endPDF(done);
});

it('Add Overlay from other PDF - fitWidth', (done) => {
const src = path.join(__dirname, 'materials/issue-28-input.pdf')
const overlayPDF = path.join(__dirname, 'materials/test2.pdf');
const output = path.join(__dirname, `output/Add overlay (#28) - fitWidth.pdf`);

const recipe = new HummusRecipe(src, output);
const options = {
keepAspectRatio: true,
fitWidth: true
}
recipe
.editPage(1)
.overlay(overlayPDF, options)
.endPage()
.endPDF(done);
});

it('Add Overlay from other PDF - fitHeight', (done) => {
const src = path.join(__dirname, 'materials/issue-28-input.pdf')
const overlayPDF = path.join(__dirname, 'materials/test2.pdf');
const output = path.join(__dirname, `output/Add overlay (#28) - fitHeight.pdf`);

const recipe = new HummusRecipe(src, output);
const options = {
keepAspectRatio: true,
fitHeight: true
}
recipe
.editPage(1)
.overlay(overlayPDF, options)
.endPage()
.endPDF(done);
});

it('Add Overlay from other PDF - stretch to fit', (done) => {
const src = path.join(__dirname, 'materials/issue-28-input.pdf')
const overlayPDF = path.join(__dirname, 'materials/test2.pdf');
const output = path.join(__dirname, `output/Add overlay (#28) - stretch.pdf`);

const recipe = new HummusRecipe(src, output);
const options = {
fitHeight: true,
fitWidth: true
}
recipe
.editPage(1)
.overlay(overlayPDF, options)
.endPage()
.endPDF(done);
});
});

0 comments on commit 95218aa

Please sign in to comment.