Question

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!

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top