galkahana / HummusJS

Node.js module for high performance creation, modification and parsing of PDF files and streams
http://www.pdfhummus.com
Other
1.15k stars 170 forks source link

Question: Images replacement #398

Closed maximuk closed 5 years ago

maximuk commented 5 years ago

Hi,

I'm trying to replace some images in source PDF, but I succeeded only in replacing with JPEG images. Here is the code example:

const hummus = require('hummus');

const pdfWriter = hummus.createWriter('./output.pdf');
const copyingContext = pdfWriter.createPDFCopyingContext('./input.pdf');

const imageXObject = pdfWriter.createImageXObjectFromJPG('./jpeg.jpeg');
const xObjectIds = pdfWriter.createFormXObjectsFromPDF('./pdf.pdf', hummus.ePDFPageBoxMediaBox);

copyingContext.replaceSourceObjects({ 41: xObjectIds[0] }); // -- does not work
copyingContext.replaceSourceObjects({ 46: imageXObject.id }); // -- works

copyingContext.appendPDFPageFromPDF(0);

pdfWriter.end();

I understand that one PDF can be rendered into another, but in this case "z-index" and all transformations will be lost. So replacement is required.

The question is how to prepare PDF/PNG/TIFF images for replacement.

Thanks in advance!