29

I have a lot of PDF documents that I want to convert to PNG, edit in Gimp, and then save back to the multipage Acrobat file. I'm filling out forms and adding scanned signature, trying to avoid printing, signing, then scanning back in, with the ability to type the information I need to enter.

I've been trying to use Imagemagick to convert to png files, which seems to work fine. I use the command convert -quality 100 -density 300x300 multipage.pdf single%d.png
(I'm not really sure if the quality parameter is right for png).

But I'm having problems with saving back to PDF. Some of the files have the wrong page size, and I've tried every command and procedure I can find, but there are always a few odd sizes. The resolution seems to vary so that it looks good at a certain zoom level, but either a few pages are specified at about 2" wide, or they are 8.5x11 but the others are about 35" wide. I've tried making sure Gimp had the canvass size and resolution correct, and to save the resolution in the file, but that doesn't seem to matter.

The command I use to save the files is convert -page letter -adjoin single*.png multipage.pdf I've tried other parameters, but none seemed to matter.

If anyone has any ideas or alternatives, I'd appreciate it.

Marty Fried
  • 487
  • 1
  • 5
  • 12
  • 4
    I think you can edit the PDFs directly with GIMP. – Blender Mar 14 '12 at 20:56
  • 3
    If what you want to do is add a signature image as a overlay to the pdf page, you can do with that with some of the pdf tools without converting to png. – Dan D. Mar 14 '12 at 21:04
  • 1
    @Blender: Gimp imports PDFs, but doesn't actually save to PDF; it will, on Linux at least, print to PDF, but only single images, not the multi-page PDFs that I desire. – Marty Fried Mar 14 '12 at 23:48
  • @Dan D: I need to add signature and date, and it needs to be in different locations on each file, not at all similar. But if you know of other tools that might work well, I'd like to know about them. – Marty Fried Mar 14 '12 at 23:53

2 Answers2

38

"I'm not really sure if the quality parameter is right for PNG."

For PNG output, the -quality setting is very unlike JPEG's quality setting (which simply is an integer from 0 to 100).

For PNG it is composed by two single digits:

  • The first digit (tens) is (largely) the zlib compression level, and it may go from 0 to 9.
    (However the setting of 0 has a special meaning: when you use it you'll get Huffman compression, not zlib compression level 0. This is often better... Weird but true.)

  • The second digit is the PNG data encoding filter type (before it is compressed):

    • 0 is none,
    • 1 is "sub",
    • 2 is "up",
    • 3 is "average",
    • 4 is "Paeth", and
    • 5 is "adaptive".

In practical terms that means:

  • For illustrations with solid sequences of color a "none" filter (-quality 00) is typically the most appropriate.
  • For photos of natural landscapes an "adaptive" filtering (-quality 05) is generally the best.

"I'm having problems with saving back to PDF. Some of the files have the wrong page size, and I've tried every command and procedure I can find [...] but either a few pages are specified at about 2" wide, or they are 8.5x11 but the others are about 35" wide."

Not having available your PNG files, I created a few simple ones with different dimensions to verify the different commands (as I wasn't sure myself any more). Indeed, the one you used:

convert -page letter -adjoin single*.png multipage.pdf

does create all PDF pages in (same) letter size, but it places my sample of (differently sized) PNGs always on the lower left corner of the PDF page. (Should a PNG exceed the PDF page size, it does scale them down to make them fit -- but it doesn't scale up smaller PNGs to fill the available page space.)

The following modification to the command will place the PNGs into the center of each PDF page:

convert           \
  -page letter    \
  -adjoin         \
   single*.png    \
  -gravity center \
   multipage.pdf

If this is still not good enough for you, you can enforce a (possibly non-proportional!) scaling to almost fill the letter area by adding a -scale '590!x770!' parameter (this will leave a border of 11 pt at each edge of the page):

convert              \
  -page letter       \
  -adjoin            \
   single*.png       \
  -gravity center    \
  -scale '590!x770!' \
   multipage.pdf

To leave away the extra border, use -scale '612!x792!'. -- Should you want only upward scaling to happen if required while keeping the aspect ratio of the PNG, use -scale '590<x770<':

convert              \
  -page letter       \
  -adjoin            \
   single*.png       \
  -gravity center    \
  -scale '590<x770<' \
   multipage.pdf 
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • Thanks. I'm just starting my vacation now, but when I get back, I'll try out some of your suggestions and see how they work, but you deserve an upvote for the effort even if it doesn't work. :) – Marty Fried Aug 20 '12 at 23:41
  • Heh :-) -- But I'm pretty sure that it works. If only I'd always get upvotes 'for the effort', I'd be rich man by now :-) – Kurt Pfeifle Aug 20 '12 at 23:43
6

Why not just use Xournal? That's what I use to annotate PDFs

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
Tim
  • 61
  • 1
  • 2
  • Thanks, the reason I don't use it is that I never heard of it before. It sounds promising, though, and I will definitely look into it when I get back from my vacation. I'll revisit this topic in a couple of weeks. – Marty Fried Aug 20 '12 at 23:40