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

PNG Support? #28

Open MOuli90 opened 10 years ago

MOuli90 commented 10 years ago

Why does HummusJS (PDF-Writer) not support images in the png format?

Is it possiable to copy the code of the ZF1 into this Project?

https://raw.githubusercontent.com/zendframework/zf1/master/library/Zend/Pdf/Resource/Image/Png.php

galkahana commented 10 years ago

hi. I just didnt write support for it. tjats the only reason. what you could do is either port the code to the lib, or use as is and import the pdf result as an image to the pdf created by hummus.

linus-amg commented 8 years ago

where would be the starting point to make a pr for png support? that would be really great for autographs

galkahana commented 8 years ago

Before you go into that then by FAR the simplest solution is to find an existing module that creates PDFs from PNGs and embedding the PDF. Seriously, the alternative of building it yourself is significantly more time consuming and involved. imagemagick can probably help here.

If you do decide to do this internal to Hummus, Best would be to do this in the C++ code where you can surely access all the relevant hooks in hummus to create the objects you need. plus this way it's accessible to everyone. Still if you prefer to focus on javascript and missing some necessary hooks that are available on PDFWriter, let me know and we'll figure out how to open them up.

In any case, since hummusJS is mostly a wrapper around the C++ library, the best way to learn on how to add images types to hummus is to look into its implementation of AbstractContentContext::DrawImage and do the JS equivalent. follow the track of how jpg images are drawn because it's the simplest.

This will get you the details on how to add an image type in general.

You will need to know 2 more things:

  1. how to specifically encode png into pdf.
  2. how to support transparency in png.

To do 1, the best is to look for an existing implementation. someone that already converts png to pdf, and use the pdfwriter/hummusjs library to do the equivalent PDF stuff. Reading png will probably take libpng. if you cant find anything then it will take reading into libpng, figuring out how to get the color samples and creating the image object by yourself. yummy. in this case better read on how to create images objects (section 4.8) in the PDF specs . Following the JPG image creation trail in hummus will show you a simple example on how to implement images objects in PDF.

In case while reading 1 you didn't stumble upon a solution on how to carry transparency information, and you want transparency (in my tiff solution i got lazy and didn't), then you'll have to learn how to:

  1. read transparency samples from PNG. libpng can help you with that. i guess.
  2. create an image mask in the PDF and use it every time you use the image you created earlier. read about them in section 4.8.5 of the PDF specs. This will be good for alpha values that are either 1 or 0. if you want to support interim values, to support something like RGBA (don't know what they have in PNG), you can use Soft-mask images which are like image marks but allow interim values. read about them in page 553.

If you manage to do this, you will have my deep admiration and thanks. it is defo possible, and i done such things myself. it's just that it's not super trivial and will take time. I would clear something like 2 weeks for this and hope to get lucky and finish sooner.

linus-amg commented 8 years ago

wow that sounds super challenging, all i really need is to add an image with mayority alpha 1 pixels (no interims), like an autograph, without putting white background above the existing pdf, sounds easier to read the pdf at the position were the image will be inserted and snapshot a background of that area and use it as the background of the jpg autograph 🍭

thank you very much for your help and describing where to take the first steps, appreciate it lots

galkahana commented 7 years ago

Folks. if anyone's looking to solve this. what's commonly done is to turn the PNG into a PDF using something like PDFKit, and then you can import it to hummus like an image.

mLuby commented 6 years ago

NM Simple image drawing from the wiki works for PNGs.

~I noticed this section about PNG images, but trying it doesn't seem to work. Did it work in the past or perhaps it's just optimistic wikiing? 😄~

~Also this issue seems to suggest some support for PNGs…~