boazsegev / combine_pdf

A Pure ruby library to merge PDF files, number pages and maybe more...
MIT License
733 stars 154 forks source link

Stamp location at x,y #123

Open bibstha opened 7 years ago

bibstha commented 7 years ago

Hi, I saw a similar issue closed in #114 but would like to open up the discussion again.

My requirement: I'd like to put a pdf (barcode) at a certain location in a blank PDF page.

pdf = CombinePdf.new
page = CombinePdf.create_page # creates an a4 document

barcode_pdf = CombinePdf.load("barcode.pdf")
page << barcode_pdf

pdf << page
pdf.save "a4_with_barcode.pdf"

In the above, I cannot set x, y so that I can layout the barcode file. Is it possible to do it?

Only way I figured:

a. resize the image so I can add padding to the top and right b. rotate 180 c. convert to string and back to pdf d. resize the image so I can add padding to top and right (now with 180 rotation) e. rotate 180 again d. use this final pdf to add it back to a4 :-/

Is there an easier way to do this?

boazsegev commented 7 years ago

Hi @bibstha ,

I'm assuming the target barcode size is static...?

In that case the easiest way to do what you want is to create a stamp PDF that actually fits your needs.

You can either use a PDF authoring program of tweak the source PDF using CombinePDF.

The "Stamp" feature overlays two PDF pages on top of each other (or underneath each other).

Adding location and sizing manipulations could be done using "ctm", but that would make the API a bit complex for my taste. I think editing the source PDF before stamping provides the simplest API.

Perhaps an "add image" feature could be added in a future release if someone takes the time for a PR (I'm swamped with other projects).

As to the rotation and padding, I would consider editing the mediabox and cropbox (if required) so the have a negative starting point (padding) before resizing. This might replace 2 steps with a cheaper and single step. Also, I'm not sure why you're converting to string and back to pdf... but I didn't test it out, so I'm not sure.

Good Luck!