Prawn - How can i allow unlimited height - ruby-on-rails

On ruby when you create pdf using Prawn you can use:
pdf = Prawn::Document.new(:page_size => [200, 900])
But I want to create a pdf with a fixed width (200) and unlimited maximum height with a minimum.
Something like this:
pdf = Prawn::Document.new(:page_size => [200, {min: 400, max: :unlimited}])
The pdf in question is a ticket so I want to know if this is possible.
Thank you.

Related

Encoding problem when adding text to existing PDF with CombinePDF

(Rails 6.0.2.2, ruby 2.7.1, combine_pdf 1.0.18)
I'm currently trying to write some text to an existing PDF with the CombinePDF gem. Unfortunately I'm running in some encoding problems.
I'm loading the existing PDF:
pdf = CombinePDF.load "#{Rails.root}/public/pdf/base.pdf"
Then I'm adding text to it:
pdf.pages[0].textbox "Straße", height: 20, width: 160, y: 527, x: 215, font_size: 12, box_color: nil, text_align: :left, text_padding: 0
When generating a new pdf out of it:
send_data pdf.to_pdf, filename: "output.pdf", type: "application/pdf"
the string gets displayed as StraˆŸe, so the ß isn't displayed correctly.
I also tried to replace it with unicode literals (\xc3\x9f) without any effect.
Anybody has an idea what else to try?
If you use HexaPDF you could do something like this:
require 'hexapdf'
require 'stringio'
doc = HexaPDF::Document.open("#{Rails.root}/public/pdf/base.pdf")
doc.pages[0].canvas.font("Helvetica", size: 12).text("Straße", at: [215, 527])
outio = StringIO.new(''.b)
doc.write(outio)
Just make sure that you use a font that contains glyphs for all characters you want to use. So better use a TrueType font instead of the builtin Helvetica (you can just provide the path to a TrueType font to the #font method).

Rails 6 Axlsx Excel Gem - Images not displaying

I don't know if it's my fault or if it just doesn't work with Rails 6, but my images in the file are not displaying:
I tried:
img = File.expand_path(Rails.root+'app/assets/images/logo.jpg')
and also like described in the examples:
img = File.expand_path('../logo.jpg', __FILE__)
sheet.add_image(:image_src => img, :noSelect => true, :noMove => true) do |image|
image.start_at 5, 20
image.end_at 7, 22
end
The image is found (at least I don't get an error for this) but the only thing I see in the generated file (the rest of the creation works fine) is:
Could it be the image size not fitting in the cells? Or is it resizing automatically?
Any suggestions or ideas? I don't know what's wrong or what else I could try
Did you try setting height and width for the image? Have a look at this old answer https://stackoverflow.com/a/47982814/1796645 (potential duplicate question)
If you found a bug, then you could report it in https://github.com/caxlsx/caxlsx/issues

Prawn: how to set document's printable dimensions

I'm generating a pdf with prawn. Basically, I generate the document and I fill it with some images.
The problem comes when I download the file and I try to print it. The dimensions are not set to the ones I previously specified.
pdf = Prawn::Document.new(page_size: "A3", margin: PAGE_MARGIN, page_layout: :landscape)
When I try to print it, the default page size is "A4" instead of "A3"
How can I solve this?
I tried to attach some metadata but it didn't work correctly.
Thanks in advance!
In the case where you're generating the document within its own class, this also works to declare the paper size:
class EnvelopePdf < Prawn::Document
def initialize(_item_array, _type_of_item)
super(:page_size => [324, 684], :page_layout => :landscape) # 4.5" by 9.5", which is No 10 envelopes
... application-specific initialization code here ...
print_the_envelopes
end
Using prawn 1.3.0:
require "prawn"
pdf = Prawn::Document.new(:page_size => 'A3')
pdf.text "Hello World!"
pdf.render_file("export.pdf")
in terminal:
pdfinfo export.pdf
outputs:
Creator: Prawn
Producer: Prawn
Tagged: no
Form: none
Pages: 1
Encrypted: no
Page size: 841.89 x 1190.55 pts
Page rot: 0
File size: 842 bytes
Optimized: no
PDF version: 1.3

How to place text on image

Is there a way to place text on image in Rails? I am using Carrierwave for image upload, but I don't think it supports watermarking.
I tried attaching image watermark and made it work but can't figure out how to watermark with text.
For example, this is good way to place image watermark.
This is the code that I user to put watermarks on a image I am sure that you can change it a bit to make it your own. Also make sure that you have Magick turned on.
Take a look at carrierwave-add-a-watermark-to-processed-images its similar
# Process files as they are uploaded:
process :resize_to_fill => [850, 315]
process :convert => 'png'
process :watermark
def watermark
manipulate! do |img|
logo = Magick::Image.read("#{Rails.root}/app/assets/images/watermark.png").first
img = img.composite(logo, Magick::NorthWestGravity, 15, 0, Magick::OverCompositeOp)
end
end

Rails 3 and PDFKit. How to specify page size?

I have been looking in the documentation but can't find the answer. How can I specify the page size of my pdf document and what are the available page sizes? I keep on looking and looking but I can't find good documentation. Please point me to a URL or let me know how can I code some page size into my PDF document.
Oh and I don't want to do that on any config file because I need to generate PDf documents of different sizes.
NOT in config file...
PDFKit.configure do |config|
config.wkhtmltopdf = `which wkhtmltopdf`.to_s.strip
config.default_options = {
:encoding=>"UTF-8",
:page_size=>"A4", #or "Letter" or whatever needed
:margin_top=>"0.25in",
:margin_right=>"1in",
:margin_bottom=>"0.25in",
:margin_left=>"1in",
:disable_smart_shrinking=>false
}
end
You can set the page size when creating a new PDF like this:
kit = PDFKit.new(source, :page_size => "Legal")
PDFKit uses WKHTMLTOPDF which in turn uses QPrinter. You can find the available sizes in the QPrinter documentation (there's a bunch), but its pretty safe to say that any size paper you want is available. Also, you can set a custom size if you can't find what you need.
NB: If you don't set a default option for page_size in a config somewhere AND don't supply one in your method call, PDFKit will use its internal default (Letter). See line 10 of lib/pdfkit/configuration.rb
Pdkit accepts custom sizes:
PDFKit.configure do |config|
config.wkhtmltopdf = `which wkhtmltopdf`.strip
config.default_options = {
:page_width => '1682',
:page_height => '2378'
}
end
The sizes must be in milimeters (wkthmltopdf documentation).
Since it's using wkhtmltopdf to generate the PDFs I'm assuming you can use the same options that it supports. In a wkhtmltopdf manual I found, it mentions the following site for a list of sizes:
http://doc.trolltech.com/4.6/qprinter.html#PaperSize-enum
To set the page size, you can use the :page_size option like so:
PDFKit.new(html, :page_size => 'Letter')

Resources