How to save a Prawn generated PDF to the public folder? - ruby-on-rails

I'm using Prawn gem to generate PDFs In my application...
app/controllers/orders.rb
class OrdersController < ApplicationController
...
def show
respond_to do |format|
format.html
format.pdf do
require "prawn/measurement_extensions"
...
#render pdf document
send_data pdf.render,
filename: "order_#{#order.id}.pdf",
type: 'application/pdf',
disposition: 'inline'
end
end
end
...
end
And It's working fine for displaying, But My questions are..
How to -Automatically- save those generated pdfs in the public folder (folder for each day) after a successful order creation? I've tried searching Prawn Documentation But I've found nothing.
How to show orders in only pdf format? I've Tried to Comment the format.html line but It didn't work

When you call pdf.render and send it to the client with send_data, you're actually dumping the contents of the pdf over the wire. Instead of that, you could dump the result of pdf.render in a file with File.new and use send_file in the controller. Check the documentation for File. Alternatively, you could attach the generated pdf to the specific order using something like Paperclip.
If you generate the pdf as a file in the server, you should use send_file instead of send_data. Read more about this in the guides.

You can use Prawn's render_file method to save the generated PDF to a file:
pdf.render_file(Rails.root.join('public', "order_#{#order.id}.pdf"))
See documentation: https://prawnpdf.org/docs/0.11.1/Prawn/Document.html

Related

Generate XML file and redirect to other view with Rails

I have a piece of code that generates an XML file. What I want to, and didn't find the solution, is to generate the XML file and ALSO redirect to another page, to give a feedback message.
My code is
def exportFiles
#files=FileToExport.getComponentToExport
recursive_tree= GitHubRepositorioService.getRecursiveTree('master')
GitHubService.updateFiles(#files, recursive_tree)
xml = Builder::XmlMarkup.new(:target=>$stdout, :indent=>2)
respond_to do |format|
format.xml { send_data render_to_string(:exportFiles), filename: 'exported_module.xml', type: 'application/xml', disposition: 'attachment' }
end
FileToExport.setComponentToExport(nil)
end
As I already use "respond_to" I can't use another redirect sentence... so, there is a way to generate (downloading) that file and redirect to other view?
Unfortunately, this is not possible via the controller as you can't send two responses.
But you could do this via javascript for instance. See this topic for more info Rails how do I - export data with send_data then redirect_to a new page?

rails: create multiple download files from a controller#action?

I would like to create 2 pdf documents from a single click.
I have it working with one file, but I can't seem to get to make 2 download files to happen w/out browser complaining about DoubleRenderError.
This is the code in my controller, where I am calling it twice and breaks. If I do it just once, everything works fine.
respond_to do |format|
format.pdf do
pdf = PrintCardsFront.new(paperSize, cardCount, #cards)
send_data pdf.render, filename: "cards_front_side.pdf", type: "application/pdf"
pdf = PrintCardsBack.new(paperSize, cardCount, #cards)
send_data pdf.render, filename: "cards_backside_side.pdf", type: "application/pdf"
end
end
(Rails 4.2)
You can only send one file in the HTTP Protocol.
Try to zip your two files and send them to the user.

Creating PDFs with Prawn - missing attribute error

I'm working on PDF creation in my rails application. I found PDFkit didn't necessarily do what I wanted to do, so I figured I'd test out Prawn.
I added it to my controller using this code:
def show
#document = Document.find(params[:id])
respond_to do |format|
format.html
format.pdf do
pdf = Prawn::Document.new(#document)
send_data pdf.render, filename:"1",
type: "application/pdf",
disposition: "inline"
end
end
end
But using this I get a Missing Attribute error. I'm assuming this is because my model is also named Documents and conflicts with the Prawn::Document.new command?
Can I just not have a documents model and use Prawn - or is there something I'm missing here?
I don't think it's about Document vs Prawn::Document, but I've never seen someone pass an ActiveRecord instance to Prawn::Document.new. I think that expects an options hash, right? And calling render before giving it any content seems suspicious. What is the actual stack trace?

Collection of activerecord objects in pdf format to zip

I have a complicated problem with creating zip file containing collection of active record object in pdf format.
I have invoices controller and in this controller I have action show which looks like this:
def show
add_breadcrumb "Inovice details"
respond_to do |format|
format.html
format.pdf do
render :pdf => "file_name"
end
end
end
I generate pdf with wicked_pdf gem. And now my goal is to from selected collection get each of invoice generate pdf file from it and add it to the zip file. I don't have any idea where to start. Thanks in advance.
I just answered a very similar question here and included a test controller method that renders multiple multiple pdfs with wickedpdf and uses rubyzip to pack them together and send single archive.
generate ZIP from generated PDFs with wicked_pdf

How to generate PDF that is returned to the client using the Prawn gem

i am using prawn gem to generate pdf file,
pdf.render_file('csv.pdf') is saving the file to the project root
instead i want to open the pdf before saving to root, then save it to any location
how to go about it??
thanks
Try in the controller:
respond_to do |format|
format.pdf do
`send_data your_pdf.render, :filename=>"default_filename.pdf", :type=>"application/pdf"`
end
end
where your_pdf is the object you created with Prawn.
You could also user prawnto with the inline => false option.

Resources