1

I'm generating PDF with the Wicked PDF gem on Ruby on Rails, but have to repeat some HTML content on every page.

What I'm trying is to use just part of the page to my main content, and use HTML to add stuff around it, in every page.

Something like this image (Check)

I tried playing with header, but I wasn't able to put the content in front of the HTML (even using z-index), and was only able to position the main content with margin and spacing vertically (didn't find any options to do it horizontally).

Any ideas? Thanks!

joaopribs
  • 648
  • 1
  • 7
  • 13

1 Answers1

0

Since no one answered, I will post my solution here. It is not beautiful nor brilliant, it is so so so far from these - but since no one answered, maybe we can start a discussion from it.

I was able to generate a PDF with only the text (but with the correct margins), with no background, using Wicked PDF. Like this image. I used it to just save the file.

This was the code for it:

# Save PDF with only the text (with correct margins)
render :pdf => "text#{id}",
       :margin => {:top => "1.6in", :left => "4.1in", :right => "1.2in", :bottom => "2.5in"},
       :page_size => "Letter",
       :template => "careers/job_pdf_text.pdf.erb",
       :save_to_file => Rails.root.join('public/job_pdf_tempfiles', "text#{id}.pdf"),
       :save_only => true,
       :no_background => true

Then I used RMagick to create images from this saved PDF. The important here is that I saved GIFs with transparent background, and Magick creates one image for each page on the PDF. This was the code to save the images:

# Save images from the only-text PDF
@text_images = []
text_images_magick = Magick::Image.read(Rails.root.join('public/job_pdf_tempfiles', "text#{id}.pdf"))
text_images_magick.each_with_index do |image, index|
    file_name = Rails.root.join('app/assets/images/careers_pdf', "text#{id}-#{index}.gif")
    image.write(file_name)
    @text_images << file_name
end

Ok, so at this moment I have images of the text, like this. Now what I did was put those in an HTML page, in the correct place and then used Wicked PDF again to render the final PDF. I rendered the PDF with margin 0, and for each @text_images I created a container, from which I positioned everything else (including the image itself) to achieve what I wanted in the beggining.

Does anyone have a better idea to share?

joaopribs
  • 648
  • 1
  • 7
  • 13
  • If the surroundings of the text is the same on all pages, it might have been possible to create the text pdf like you did and then add a background/stamp it with a static background for each page using some other tool if something like that is available. That way the background area marked with HTMLHTMLHTML in the example would have been a presaved image. See http://etutorials.org/Linux+systems/pdf+hacks/Chapter+6.+Dynamic+PDF/Hack+90+Superimpose+PDF+Pages/ itext is handy, not sure if pdftk might be able to do it as well. – Joel Peltonen Apr 04 '13 at 18:30
  • pdftk looks great for me! I will try to implement something with it and will come back if I have something useful. Thanks! – joaopribs Apr 05 '13 at 00:49
  • I tried using pdftk, but with no avail. The thing is I can run it on my shell, but I'm not able to run it from my app. I tried using backticks (`) and system(), but always get something like this: /Users/joaopaulo/.rvm/gems/ree-1.8.7-2012.02@eci.com/gems/activemodel-3.1.0/lib/active_model/attribute_methods.rb:302: command not found: pdftk – joaopribs Apr 10 '13 at 21:18
  • I was able to do it with pdftk! To run it, I needed to use the absolute path of the pdftk program. My resulting PDFs were a lot smaller and nicer, thanks a lot – joaopribs Apr 16 '13 at 22:48
  • Awesome, that is really cool. Would you mind adding the solution as an edit to this one? I would be interested in seeing how you did it in the end :) – Joel Peltonen Apr 17 '13 at 05:44