Rails render multiple pdf files to a folder -
i have controller action renders pdf download. want render multiple pdfs tmp folder ( zip them download )
i can generate pdfs , present user can't figure out how create folder store them in.
i'm using prawn. has render_file
method save filesystem don't know directory or if other users save pdf's same folder, need create uniques folder each user save pdf's there.
how can this?
my controller action currently
def showpdf respond_to |format| format.html format.pdf @items.each |pdf| pdf = prawn::document.new(page_size: "a4",margin: [0,0,0,0]) # pdf creation stuff... # used render 1 pdf browser # need save multiple pdf's #send_data pdf.render, filename: 'report.pdf', type: 'application/pdf' end end end
you need store files tmp/your-folder folder,
require 'prawn' @items.each |item| pdf = prawn::document.new pdf.text("lets zip all.") pdf.render_file('tmp/your-folder/#{item.id}.pdf') end
and use https://github.com/rubyzip/rubyzip zip your-folder.
require 'zip' folder = "tmp/your-folder/" zipfile_name = "tmp/archive.zip" input_filenames = dir.entries("tmp/your-folder/").select {|f| !file.directory? f} zip::file.open(zipfile_name, zip::file::create) |zipfile| input_filenames.each |filename| zipfile.add(filename, folder + '/' + filename) end zipfile.get_output_stream("myfile") { |os| os.write "myfile contains this" } end
simply send file user. if pdf contains lot of data move them delayed jobs. hope makes sense if not please hit reply.
Comments
Post a Comment