boazsegev / combine_pdf

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

Footer template with page numbering #47

Closed crysper closed 8 years ago

crysper commented 8 years ago

Thanks for the quick reply for my previous answer.

I have another question or 2. Can you tell me please if I can achieve this with combine_pdf?

1) Can I use a footer pdf template and somehow apply the page numbering on it? I want to generate a pdf footer template with another library from HTML and put it over existing footer of other pdf documents(unfortunately I won't know the format of the other pdfs, but I can generate the HTML footer template on the fly)

A very slow method would be to iterate each page, get the current page number, replace the page numbering placeholder from the HTML footer with the current page number, convert the HTML template to PDF than merge it. However, the performance will be incredibly low.

I'm thinking if I can calculate the position of the page numbering placeholder on the footer and use the coordinates in your page_numbering function. But I'm not sure if how it works....

The footer will also have links or links with images to external websites.

2) I want to replace first and last page with my own pdf pages. Does this break the TOC or other bookmarks?

Thank you

boazsegev commented 8 years ago

@crysper , frankly I don't know how to answer these questions, as some of them depend on the libraries you use.

I think the dynamic footer approach isn't going to be easy to implement. Here are a few pointers:

  1. All the text in a PDF is encoded. So, replacing a predefined text string (such as a place holder) isn't possible.
  2. PDF files can have different "modes" - such as "screen" and "paper". Html->PDF libraries sometimes use these modes in correlation to the CSS @media rules. CombinePDF doesn't support these PDF features and it will throw an exception... so you'll have to test it out.
  3. Link survival is partial when using CombinePDF. Named links should survive, however, bookmarks are discarded as well as the TOC (in the future, TOC merging might be available).
  4. The X,Y coordinates are relative to the bottom left of the PDF page (this is part of the PDF standard). You can use the :center flag to make the alignment a bit easier... but you will mostly have to test things out and remember that the pages might change is sizes (even within the same file).

Good Luck!

P.S.

StackOverflow is a good place for Q&A about programming. Perhaps someone else already faced a similar challenge and could help more then I.