1

I'm trying to edit an existing PDF using Hummus js (now Muhammara js). In the example provided in the documentation page of the library for editing existing PDF, the editing is done by chaining of methods.

const Recipe = require("muhammara").Recipe;
const pdfDoc = new Recipe("input.pdf", "output.pdf");

pdfDoc
  // edit 1st page
  .editPage(1)
  .text("Add some texts to an existing pdf file", 150, 300)
  .rectangle(20, 20, 40, 100)
  .comment("Add 1st comment annotaion", 200, 300)
  .image("/path/to/image.jpg", 20, 100, { width: 300, keepAspectRatio: true })
  .endPage()
  // edit 2nd page
  .editPage(2)
  .comment("Add 2nd comment annotaion", 200, 100)
  .endPage()
  // end and save
  .endPDF();

In my use-case, I can't chain multiple methods as the parameters to be passed to each of these methods is in a map.

{"2":[
    [{"x":5.593,"y":3.401},{"x":6.385,"y":3.401},{"x":6.385,"y":3.54},{"x":5.593,"y":3.54}],[{"x":5.58,"y":2.488},{"x":6.379,"y":2.488},{"x":6.379,"y":2.627},{"x":5.58,"y":2.627}]
]}

The key is the page number in which a polygon has to be drawn. The values are arrays for vertices of the polygon.

I tried to execute the below code snippet.

const Recipe = require("muhammara").Recipe;
let pdfDoc = new Recipe(inputFile, outputFile);

pdfDoc
  .editPage(1)
  .polygon(
    [ [400.459, 86.998], [457.166, 86.998], [457.166, 98.412], [400.459, 98.412] ],
    { stroke: [150, 0, 0], lineWidth: 1, })
    
pdfDoc
  .editPage(1)
  .polygon(
    [ [399.528, 63.643], [456.736, 63.643], [456.736, 73.031], [399.528, 73.031] ],
    { stroke: [150, 0, 0], lineWidth: 1 }
  )
  .endPage();

pdfDoc.endPDF(() => { console.log('done') });

But, it is throwing error `Unable to end PDF`.
Error Log:

[ 14/03/2023 21:57:25 ] ObjectsContext::WriteXrefTable, Unexpected Failure. Object of ID = 103 was not registered as written. probably means it was not written
[ 14/03/2023 21:57:25 ] PDFWriter::EndPDF, Could not end PDF
[ 14/03/2023 23:01:06 ] ObjectsContext::WriteXrefTable, Unexpected Failure. Object of ID = 103 was not registered as written. probably means it was not written
[ 14/03/2023 23:01:06 ] PDFWriter::EndPDF, Could not end PDF

It works if I chain the polygon().polygon() one after another. But, I cant do as I need to get the parameters from a map as explained earlier.

  • What if you call `endPage()` in the first chain before starting the second chain? – Eric Ferreira Mar 15 '23 at 20:35
  • If the `endPage()` is invoked in the first chain before starting the second chain, then the modification can't be done again on the same page. Further modifications is simply ignored for the same page. – Debadatta Meher Mar 16 '23 at 06:12

1 Answers1

0

Since, the modifications happen with chaining like below :

pdfDoc.editPage(1).polygon(parameter1).polygon(parameter2).polygon(parameter3).endPage()
.editPage(2).polygon(parameter4).polygon(parameter5).endPage()
.endPDF(() => { console.log('done')})

I created a string dynamically and added the chaining methods and parameters using string template, let's say the string is evalString. Finally used eval(evalString)

However, I was reluctant to use eval() function as it is suggested to avoid using this function.