almo
almo

Reputation: 6367

Path to temporary Wicked PDF file in Rails 5

I am creating a PDF file using Wicked:

pdf = WickedPdf.new.pdf_from_string('<h1>Hello There!</h1>')

I assume that this creates a temporary file somewhere. How can I get the path to this temp file?

Upvotes: 1

Views: 2706

Answers (3)

new2cpp
new2cpp

Reputation: 3677

Please refer to example of https://github.com/mileszs/wicked_pdf#super-advanced-usage.

The official example use File

pdf = render_to_string pdf: "some_file_name", template: "templates/pdf", encoding: "UTF-8"

# then save to a file
save_path = Rails.root.join('pdfs','filename.pdf')
File.open(save_path, 'wb') do |file|
  file << pdf
end

You may use tempfile to do it as

pdf_string = WickedPdf.new.pdf_from_string(...)

overlay = Tempfile.new('overlay')
overlay.binmode
overlay.write(pdf_string)
overlay.close
overlay.path # to get path

Upvotes: 1

Athul Santhosh
Athul Santhosh

Reputation: 246

you can do this when you are creating the pdf only u can pass in the option of your desired temp path

pdf = WickedPdf.new.pdf_from_string('<h1>Hello There!</h1>', {temp_path: "your path here")

Refer this link for more inputs this contains the function that you are using and the inputs that can be passed

Upvotes: 1

Reuben Mallaby
Reuben Mallaby

Reputation: 5763

Checking the source of WickedPDF it has a TempFile

The temporary file should be created in the temporary directory as defined in options, or in Dir.tmpdir.

Upvotes: 0

Related Questions