Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从image/docx/xlsx/csv文件生成PDF文件并存储到s3 rails 6.0中_Ruby On Rails_Pdf Generation_Wicked Pdf - Fatal编程技术网

Ruby on rails 从image/docx/xlsx/csv文件生成PDF文件并存储到s3 rails 6.0中

Ruby on rails 从image/docx/xlsx/csv文件生成PDF文件并存储到s3 rails 6.0中,ruby-on-rails,pdf-generation,wicked-pdf,Ruby On Rails,Pdf Generation,Wicked Pdf,我正在仅API模式下使用rails 6应用程序。因此,对此没有任何看法 我正在s3上存储上传的文件,现在我想生成上传文件的pdf。 为此,我使用上传文件的S3URL,并尝试将新的PDF文件存储在s3上 下面是我在控制器上的代码 if @attachment.save url = @attachment&.attachment&.service_url template = File.open(Rails.root.join('app/views/b

我正在仅API模式下使用rails 6应用程序。因此,对此没有任何看法

我正在s3上存储上传的文件,现在我想生成上传文件的pdf。 为此,我使用上传文件的S3URL,并尝试将新的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:
但它不工作,我有以下错误
“不支持文件类型PNG图像(图像/PNG)” 当我从/tmp文件夹打开pdf文件时,得到了一个空白文件

编辑

这段代码可以很好地处理jpg/png这样的图像文件,并且能够用图像生成pdf

但是如何使用windows文件(docx/xlsx)或csv/text文件实现这一点呢

我尝试用下面的代码打开这些文件,但是得到了一个小文件的Readtimeout错误

    url = Rails.application.routes.url_helpers.url_for(@attachment.attachment.attachment)
    data = open("#{url}").read

很明显,您只发布了部分代码,但看起来并没有呈现任何响应请求的内容,这就是您收到此错误的原因。您只需添加一些简单的内容,如
render json:{status:“OK”data:“Upload Complete”}
    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
    url = Rails.application.routes.url_helpers.url_for(@attachment.attachment.attachment)
    data = open("#{url}").read