HummusJS icon indicating copy to clipboard operation
HummusJS copied to clipboard

TypeError: Unable to end PDF when adding an image

Open rubenjimenez opened this issue 7 years ago • 14 comments

Hi,

I've been looking for this problem in the general list of issues and I haven't found how to fix it.

I've got a PDF I'm using it to modify. I add text and everything works like a charm, but when I've tried to add an image I'm getting this error:

TypeError: Unable to end PDF

This is my code:

  var certificateName = transaction._id + '.pdf';
  var certificatePath = __dirname + '/output/';
  var barcodePath = __dirname + '/barcode/';

  var pdfWriter = hummus.createWriterToModify(__dirname + '/pdfs/aed_invoice_mockup.pdf', {
    modifiedFilePath: certificatePath + certificateName
  });

  var pageModifier = new hummus.PDFPageModifier(pdfWriter, 0);

  var ctx = pageModifier.startContext().getContext();

  var myriadFont = pdfWriter.getFontForFile(__dirname + '/pdfs/MyriadPro-Black.otf');
  var cheddarFont = pdfWriter.getFontForFile(__dirname + '/pdfs/cheddar_jack.ttf');

    left = calculateLeftForCentering(myriadFont, 25, product, 0, 595);

    ctx.writeText(
      product,
      left, 490,
      {
        font: myriadFont,
        size: 25,
        colorspace: 'rgb',
        color: 0x192445
      }
    );

    left = calculateLeftForCentering(myriadFont, 18, subscription, 0, 595);

    ctx.writeText(
      subscription,
      left, 450,
      {
        font: myriadFont,
        size: 18,
        colorspace: 'rgb',
        color: 0xE94F3D
      }
    );

    var amount = transaction.amount.toString() + '€';

    left = calculateLeftForCentering(cheddarFont, 60, amount, 0, 595);

    ctx.writeText(
      amount,
      left, 400,
      {
        font: cheddarFont,
        size: 60,
        colorspace: 'rgb',
        color: 0x192445
      }
);

ctx.drawImage(10, 10, '/Users/wh3s/Documents/y3gw/Projects/aed/packages/custom/aed-payments/server/controllers/barcode/barcode-599460040d507f69c48a5b72.jpg');

pageModifier.endContext().writePage();
pdfWriter.end();

Although the code may seem a little confusing, it is quite simple. I write some text several times and finally I add the image. I write the page and end the context afterwards. Here it is where I'm getting the error.

As I said, If I remove the drawImage line all the code works as expected.

Any clue about what I'm doing wrong?

Thanks!

* I've checked the image is accesible and it exists.

rubenjimenez avatar Aug 16 '17 17:08 rubenjimenez

Code seems fun. How about you include the image? Maybe hummus isnt happy with it. Also try creating a log, maybe itll have useful insights

galkahana avatar Aug 16 '17 17:08 galkahana

If you don't see any weird in the code, I'm starting to think that the image could be the problem. I'll try with another different one. I'll post the results.

I put some console.logs but they didn't return anything interesting...

rubenjimenez avatar Aug 16 '17 17:08 rubenjimenez

a log:

hummus.createWriterToModify(__dirname + '/pdfs/aed_invoice_mockup.pdf', { modifiedFilePath: certificatePath + certificateName, log: _dirname + '/logs/aed_invoice_mockup.txt' });

galkahana avatar Aug 16 '17 18:08 galkahana

Ah, I didn't know that option exists.

This is the log I'm getting:

[ 17/08/2017 13:01:42 ] ObjectsContext::WriteXrefTable, Unexpected Failure. Object of ID = 22 was not registered as written. probably means it was not written
[ 17/08/2017 13:01:42 ] PDFWriter::EndPDF, Could not end PDF

But It remains a mystery for me. Is it clearer for you?

rubenjimenez avatar Aug 17 '17 12:08 rubenjimenez

Well, object 22 that wasnt written is prbbly the one containing the image data. It wasnt wrutten prbbly because it didnt like the image.

galkahana avatar Aug 17 '17 12:08 galkahana

That's correct. Finally, it is the image. I don't know why, but it is only that precisely image.

I've tried with another different images and it works.

Thanks Gal.

rubenjimenez avatar Aug 17 '17 12:08 rubenjimenez

wanna send me the image? i can look and see what's the problem with it

galkahana avatar Aug 17 '17 12:08 galkahana

This one for example: barcode-59945dd6489be069692a9f94

rubenjimenez avatar Aug 17 '17 12:08 rubenjimenez

easy one. aint no jpg. its a png. pngs not supported by hummus.

galkahana avatar Aug 17 '17 13:08 galkahana

True.

I have used Sharp to convert the image to JPEG and that's the way I've been able to add the image to the PDF.

Thanks Gal for your insights.

Anyway, is there any plan to support PNG in Hummus?

rubenjimenez avatar Aug 18 '17 09:08 rubenjimenez

not right now. png would be great. it supports transparency and its super common, but i'm busy on other projects. what i am looking at adding next to hummus is support for webassembly

galkahana avatar Aug 18 '17 15:08 galkahana

pngs are now supported

galkahana avatar Sep 11 '17 10:09 galkahana

Great news. Thanks for your dedication!

rubenjimenez avatar Sep 11 '17 10:09 rubenjimenez

I know this project is no longer actively maintained, but I would greatly appreciate it if anyone could point me to a solution or even just a reason as to why this still occurs, although only on certain images.

20kaushik02 avatar Feb 07 '22 13:02 20kaushik02