galkahana / HummusJS

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

Add and remove image #395

Closed pjm4 closed 5 years ago

pjm4 commented 5 years ago

Hi,

I need to add an image to an existing PDF and then allow the user to be able to remove that same image later. At the moment, I'm using the following code to add an image (which works well):

var hummus = require('hummus');

var pdfWriter = hummus.createWriterToModify(__dirname + '/input.pdf', {
  modifiedFilePath: __dirname + '/output.pdf'
});

// Add image to bottom of page.
var pageModifier = new hummus.PDFPageModifier(pdfWriter, 0)
pageModifier.startContext().getContext().drawImage(178, 61, './A.png', {transformation:{width:100,height:100, proportional:true}});
page4Modifier.endContext().writePage();
pdfWriter.end();

Workflow required is:

  1. read existing PDF, add image 'A', save PDF (code above does this)
  2. read new PDF remove image 'A', save PDF (need to figure this bit out)

Ideally, when the image is added I'd include an ID. Later on I could find the image by this ID and remove it.

Could you provide some pointers?

Thanks,

Philip

galkahana commented 5 years ago

You can use a form and place the image in the form. Should give you the id of the form. When wanting to remove replace the form object with and empty one.ake sure to use the same size for form and image so the image will show.

https://github.com/galkahana/HummusJS/wiki/Reusable-forms

pjm4 commented 5 years ago

Thanks @galkahana - I've started using pdf-lib as it is working within the browser. Your suggestion of the empty form has worked very well with this library, so many thanks for your help.