Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6327687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:19:29+00:00 2026-05-24T17:19:29+00:00

I’m creating a pdf book where I need to put a background image for

  • 0

I’m creating a pdf book where I need to put a background image for each page.

The size of the page is (576 x 576) and the size of the background image is 2700 x 2700 (300 dpi.) (These sizes are the requirements so cannot be adjusted).
My problem is – the background image appears out of proportion in the page. How can I fix this ?
Here’s my code:

 Prawn::Document.generate("#{Rails.root.to_s}/public/#{filename}.pdf", :page_size => [576,576], :left_margin => 50, 
:right_margin => 50, :page_layout => :portrait, :skip_page_creation => true,   :skip_encoding => true, 
:background => "#{Rails.root.to_s}/public/images/pdf/bg_blank_low.jpg" ) do |pdf|
    ....
    ....
    ....
)

Is there any other way by which I can place the image of 300 dpi as a background image.

I even tried adding pdf template as a background, but still no luck.

Any suggestion or hint will be much appreciated.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:19:29+00:00Added an answer on May 24, 2026 at 5:19 pm

    What you’re trying to do can’t be done using the :background option of Prawn::Document.generate since the :background option can’t scale the image. If you want to use the :background option, you need to make sure your image is the same dpi as the PDF (normally 72dpi).

    You can however simply imbed the image in the page, like you would a normal image and then float the text over the top of it. This works since when you embed an image you’re able to scale it. The code might look something like this:

    Prawn::Document.generate("#{Rails.root.to_s}/public/#{filename}.pdf", :page_size => [576,576], :left_margin => 50, :right_margin => 50, :page_layout => :portrait, :skip_page_creation => true, :skip_encoding => true) do |pdf|
      bg_image = "#{Rails.root.to_s}/public/images/pdf/bg_blank_low.jpg"
      pdf.image bg_image, :scale => 0.2311
      pdf.move_up 576
    end
    

    This will make your ‘background’ fully cover the page (since we manually calculated the scale 2700/576), if you want to respect your margins (this is probably a better way in general), you might alter to something like:

    pdf.image bg_image, :width => pdf.bounds.width
    

    This should automatically scale the image based on the width of the bounding box of the page. Of course you would need to change your move_up as well, something like:

    pdf.move_up pdf.bounds.height
    

    After you did this you can start putting your text in and it should appear over the top of your image and so we get our simulated scaled background.

    Update

    This is an update with regards to the comment. If you have pages being automatically created and you want them to have the same background then you’re out of luck if you’re using the current prawn release the way it is. If you really need this functionality, then you have to patch prawn.

    Grab the prawn source (from https://github.com/sandal/prawn) and have a look at it. What you’re after is lib/document.rb, on line 244 there is a method start_new_page, this is the one you’re after. Within this method on line 280, you can see where the background is being set. Unfortunately it is using canvas which means, your image already has to be of the right size. This is why the background image doesn’t scale automatically.

    You will need to override this behaviour. Since this is Ruby, all you have to do is reopen the class within your project and then copy paste this method in (if you need more info on how to do this, there is plenty around on monkey-patching Ruby classes). Now you edit this method to your hearts content. The easiest way is probably to remove the canvas all together and then use our image trick from above. So the line ends up as:

    image(@background, :width => bounds.width) if @background
    move_up bounds.height
    

    You can now go back to using the standard way of setting the background and everything should work.

    Infact, you may even be able to get away with changing line 280 to this:

    canvas { image(@background, :width => bounds.width) } if @background
    

    And everything should work fine, saving you having to type an extra line :). Using image with the :width option should automatically scale the image, while using the :at option as prawn does won’t scale the image.

    Note: I haven’t actually done this so you may need to work the kinks out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.