I am working on rails 6 application in API only mode. So there is no view for this.
I am storing the uploaded files on the s3 now I want to generate the pdf of the uploaded file. For that, I am using the s3 url of the uploaded file and tried to store that new PDF file on s3.
following is my code on controller
if @attachment.save
url = @attachment&.attachment&.service_url
template = File.open(Rails.root.join('app/views/bx_block_bulkuploading/index.html.erb'))
pdf_string = WickedPdf.new.pdf_from_string(ActionController::Base.new.render_to_string(:template => template,:layout => false))
tempfile = Tempfile.new(["#{@attachment&.attachment_blob&.filename.to_s}", ".pdf"], Rails.root.join('tmp'))
tempfile.binmode
tempfile.write pdf_string
tempfile.close
end
I am getting following error
*** ActionView::MissingTemplate Exception: Missing template /#<File:0x00007f236ccf4fe0> with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :vtt, :png, :jpeg, :gif, :bmp, :tiff, :svg, :mpeg, :mp3, :ogg, :m4a, :webm, :mp4, :otf, :ttf, :woff, :woff2, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :gzip, :pdf], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :haml]}. Searched in:
EDIT
I have tried following as well
url = Rails.application.routes.url_helpers.url_for(@attachment.attachment.attachment)
data = open("#{url}").read
@base64_encoded = Base64.encode64(data)
file = Tempfile.new(["#{@attachment&.attachment_blob&.filename.to_s.sub(/\..*/, '')}", ".pdf"], Rails.root.join('tmp'))
File.open(file, 'wb') do |f|
f.binmode
f.write Base64.decode64(@base64_encoded)
f.rewind
end
but it's not working I got following error
"File type PNG image (image/png) is not supported"
while I open the pdf file from /tmp folder, and got a blank file
Edit
pdf_string = ActionController::Base.new.render_to_string(template: 'bx_block_bulkuploading/index.pdf.erb', layout: 'layouts/application.pdf.erb', locals: {attachment: @attachment})
pdf_string = WickedPdf.new.pdf_from_string(pdf_string)
tempfile = Tempfile.new(["#{@attachment&.attachment_blob&.filename.to_s.sub(/\..*/, '')}", ".pdf"],
Rails.root.join('tmp'))
tempfile.binmode
tempfile.write pdf_string
tempfile.close
This code works fine with the image file like jpg/png and able to generate pdf with the image.
but how to achieve that with the windows file(docx/xlsx) or csv/text file?
I try to open those files by the following code but get the Readtimeout error for a small file.
url = Rails.application.routes.url_helpers.url_for(@attachment.attachment.attachment)
data = open("#{url}").read