如何解决从 image/docx/xlsx/csv 文件生成 PDF 文件并存储到 s3 rails 6.0
我正在以仅 API 模式开发 rails 6 应用程序。所以没有这个观点。
我将上传的文件存储在 s3 上,现在我想生成上传文件的 pdf。 为此,我使用上传文件的 s3 url 并尝试将新的 PDF 文件存储在 s3 上。
以下是我在控制器上的代码
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
我收到以下错误
*** 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:
编辑
我也试过了
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(/\..*/,'')}",Rails.root.join('tmp'))
File.open(file,'wb') do |f|
f.binmode
f.write Base64.decode64(@base64_encoded)
f.rewind
end
但它不起作用我收到以下错误
“不支持文件类型 PNG 图像(图像/png)”
当我从 /tmp 文件夹中打开 pdf 文件时,得到一个空白文件
编辑
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(/\..*/,Rails.root.join('tmp'))
tempfile.binmode
tempfile.write pdf_string
tempfile.close
此代码适用于 jpg/png 等图像文件,并且能够使用图像生成 pdf。
但是如何使用 windows 文件(docx/xlsx)或 csv/text 文件来实现?
我尝试通过以下代码打开这些文件,但收到一个小文件的 Readtimeout 错误。
url = Rails.application.routes.url_helpers.url_for(@attachment.attachment.attachment)
data = open("#{url}").read
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。