Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to set height of page size to auto? #974

Closed
vonchristian opened this issue Aug 14, 2016 · 6 comments
Closed

how to set height of page size to auto? #974

vonchristian opened this issue Aug 14, 2016 · 6 comments

Comments

@vonchristian
Copy link

Im using prawn to generate receipts and the height of the page is unknown because it depends on how many items a customer purchases.

@pointlessone
Copy link
Member

Hi @vonchristian,

I'm afraid there's no way to set 'auto' for a page height. PDF's main use case is a paged documents.

As a workaround I can suggest you setting a really big height for your page, laying out everything you want to wit in, recording the amount of space it took, manipulate page object to reflect the size. I can't give you the exact instructions for how to do that of top of my head. I just ccan tell you that you probably would need to get pretty deep into PDF::Core guts.

@gettalong
Copy link
Member

Just what I wanted to post 😄 - so here it goes:

You could probably use a a very large height for your document so that prawn doesn't automatically create a new one. And once you are finished with everything, use Prawn::Document#y to determine your current vertical position.

Then you can use Prawn::Document#page (a PDF::Core::Page object) to adjust the MediaBox for the page, something like:

require 'prawn'

Prawn::Document.generate("test.pdf", page_size: [100, 2000], margin: 10) do |doc|
  rand(100).times do
    doc.text("some text")
  end
  doc.page.dictionary.data[:MediaBox] = [0, doc.y - 10, 100, 2000]
end

@pointlessone
Copy link
Member

@gettalong Thanks! It turned out much simpler than I anticipated. 😅

@pointlessone
Copy link
Member

@vonchristian I will close this issue now. Feel free to reopen if you think this issue is not resolved for you.

@duarte171183
Copy link

Thank! in my case, i use class TicketPdf < Prawn::Document and the solution is self.page.dictionary.data[:MediaBox] = [0, self.y - 10, 350, 2000] in the method initialize.

@halo
Copy link

halo commented Nov 8, 2018

Thank you, works splendidly!

Any suggestions on how to determine the width (right-most x of the content)? In the examples above it has been hard-coded.

I don't think I can do this with PDF::Inspector in a simple way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants