Problems with Prawnto options - ruby-on-rails

I'm using Prawnto to generate PDFs in my Rails app. I want three specific options set for my PDFs:
I don't want it to start with a blank page
I want it to download directly (not inline)
I want to specify the filename
Here's my controller method:
def print
#purchase = Purchase.find(params[:id])
prawnto :prawn=>{:skip_page_creation=>true}, :inline=>false, :filename=>#purchase.deal.name + "-" + #purchase.customer.name+".pdf"
end
Without the :skip_page_creation option, the other two options (inline and filename) work fine. But when I add the skip_page_creation option, it goes inline with a default filename. And of course, if I remove skip_page_creation, I get a nice downloaded PDF with a first blank page.
The docs for this library leave something to be desired, but can anyone point me in the right direction?
Cheers!
Aaron.

I've just tried this by changing one of my inline examples which worked ok:
module SharedPdfs
def show
prawnto :prawn => {:skip_page_creation=>true}, :inline => false, :filename => "results_pdf.pdf"
render :template => '/results/show'
end
end
Had a quick look at the prawnto source and it should pickup your prawn options not sure why it isn't but at least you've got it working for now.

Related

RoR Prawn PDF back of sheet

I want to be able to put a default back of sheet on my generated PDF,
has someone already did this ?
I was wondering if there is something in Prawn to do that, something like an event which is triggered when a new page has been started ?
I made many search about this and cannot see any way to do it, your help will be precious.
Here some code to show you the way I generate my PDF :
Prawn::Document.generate(invoice_file, :print_scaling => :none, :top_margin => margin_top) do |pdf|
pdf.font_size=11
pdf.define_grid(:columns => 12,:rows => 16, :gutter => 10)
pdf.font "Helvetica"
#some code to fill my pdf
#And what I was trying to deal with it :
pdf.repeat(:all, :dynamic => true) do
pdf.start_new_page
#some code to fill my new page
end
end
This is almost working, I get the new created page but it's generating another blank page that I don't need, because of the new one I think. If someone has an idea, I take !
Thanks ! Flo.

PDFkit javascript issues

I have been looking into using PDFKit to generate pdf reports for a Rail
3 app I'm working on. Basically if I use PDFKit as middleware any
page in the app is nicely rendered to pdf including the javascript generated graphs.
However, I want to use a different layout for pdf reports that removes any of
the sidebar or navigation details so instead of using the middleware option I have been playing around with adding the following to the relevant controller action
format.pdf {
html = render_to_string(:action => "show.html.erb", :layout => "report.html.erb")
kit = PDFKit.new(html)
kit.stylesheets << "#{Rails.root}/public/stylesheets/application.css"
send_data kit.to_pdf, :filename => "file.pdf", :type => :pdf}
(I also tried the neater option of extracting this functionality to a renderer option as Katz describes here but the logic and the problem are the same)
This definitely solves my layout problem but it appears that none of the app's javascripts are being run and the graphs are no longer appearing. I had a look at the PDFKit source but I couldn't spot any major differences in the way the pdfs are produced.
I'm still finding my feet with a lot of things with rails so I'm sure it is probably something pretty obvious that is staring me in the face. Any pointers that anyone might have would be greatly appreciated
This basically is the solution I went with which is roughly based on Katz's really great blog post
ActionController.add_renderer :pdf do |template, options|
html = render_to_string template, options
html = translate_paths(html, env)
kit = PDFKit.new(html)
css_files = Array.wrap(options.delete(:css)).each do |css_sheet|
kit.stylesheets << "#{Rails.root}/public/stylesheets/#{css_sheet}.css"
end
send_data kit.to_pdf, :filename => options[:filename], :type => Mime::PDF
end
The translate_paths method is basically the same as the one used in PDKKit rack middleware code which can be seen here and below
def translate_paths(body, env)
# Host with protocol
root = "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
body.gsub(/(href|src)=(['"])\/([^\"']*|[^"']*)['"]/, '\1=\2' + root + '\3\2')
end
add :print_media_type => true as an option with the middleware for example: config.middleware.use "PDFKit::Middleware", :print_media_type => true
add :media => "all" while including your stylesheets
in you stylesheet file add the following
#media print {
#id_of_divs_to_hide{
display: none;
}
}

How can I setup a user-defined template in prawn - prawnto?

I'm using prawn with prawnto in a rails 3 application to generate some pdf's. I have created a pdf.prawn template for a specific controller's action, and it is working fine.
I have the following code in my 'document' controller, as suggested by prawnto documentation:
respond_with(#document) do |format|
format.html
format.pdf { render :layout => false } # Add this line
end
What I would like to achieve is to allow each user of the app to select different templates, and accordingly, be able to render the corresponding XXXX.pdf,prawn templates.
How can I define dynamically the prawn template name to be rendered, based on a variable, instead of the default show.pdf.prawn template?
I have been looking for a couple of days for this feature, but have
not found anything similar.
I am willing to drop prawnto and use plain prawn if this could solve this issue.
Thank you very much for your time!
Alex
I guess you can use,
render :template => "path/to/xxx.pdf.prawn", :layout => false

rails how to render a file with correct filename

This is tough one to explain so i'll try my best, and hopefully edit the question if people need more information. I am not providing exact code, but merely an example of the issue.
I am using rails 2.3.8. I am on Unix.
I have a bunch of files under a directory not Apache accessible. (i.e. /data/files/file.rpk)
I have the following in my view.
link_to "RPK File", :controller => 'mycontroller', :action=> 'myaction', :file => '/data/files/file.rpk'
I have the following in my controller.
def myaction
if FileTest.exists?(params[:file])
render :file => params[:file]
end
end
When i select the link on the page i get a download prompt for my desired file, but the name of the file is "myaction" instead of the filename.
Thoughts on how i could get it named correctly?
Sounds like a job for send_file. The x_sendfile option prevents that your workers keep busy while transferring the actual file. You can read more about that in this blogpost.
send_file path_to_file_on_filesystem, :type => "application/zip", :x_sendfile => true
You want to use send_data with the :filename option. See the API documentation.
You want to be extremely careful with this, though. Never ever trust the client/user! They will send file=../../../../etc/group or something in order to read arbitrary files on your system, so be very sure to sanitize that value before passing it to any file-reading methods.

Saving XML files with Rails

im working on a Rails project that should create XMl files, or to be more specific
use existing XMl templates and put content from the database in it.
So i dont need to create the xml structure, basically just rendering a template with content.
What would be the smartest way to do that?
So far i have a file.xml.erb in my layout folder
and i have a custom route "/renderXML" that does
def renderXML
#reading_question = ReadingQuestion.find(params[:id])
render :file => 'layouts/question.xml'
end
This works, but i also want to save the file, not only show it (actually viewing it is not really needed).
For saving i found this
File.open('fixed.xml','w'){|f| f.write builder.to_xml}
How do i access the rendered file and save it with some method like above?
Perhaps something like:
s = render_to_string :file => 'layouts/question.xml'
File.open('fixed.xml','w'){|f| f.write s}
render :text => s
Another approach :
send_data fixed, :type => 'text/xml; charset=UTF-8;', :disposition =>
"attachment; filename=fixed.xml"

Resources