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

TypeError: Unable to end PDF when adding an image #188

Open rubenjimenez opened 7 years ago

rubenjimenez commented 7 years ago

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.

galkahana commented 7 years ago

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

rubenjimenez commented 7 years ago

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...

galkahana commented 7 years ago

a log:

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

rubenjimenez commented 7 years ago

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?

galkahana commented 7 years ago

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

rubenjimenez commented 7 years ago

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.

galkahana commented 7 years ago

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

rubenjimenez commented 7 years ago

This one for example: barcode-59945dd6489be069692a9f94

galkahana commented 7 years ago

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

rubenjimenez commented 7 years ago

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?

galkahana commented 7 years ago

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 commented 7 years ago

pngs are now supported

rubenjimenez commented 7 years ago

Great news. Thanks for your dedication!

20kaushik02 commented 2 years ago

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.