title: Generate PDF file from image/docx/xlsx/csv file and store into s3 rails 6.0
body: <p>I am working on rails 6 application in API only mode. So there is no view for this.</p>
  <p>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.</p>
  <p>following is my code on controller</p>
  <pre><code>if @attachment.save
          url = @attachment&amp;.attachment&amp;.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 =&gt; template,:layout =&gt; false))
          tempfile = Tempfile.new([&quot;#{@attachment&amp;.attachment_blob&amp;.filename.to_s}&quot;, &quot;.pdf&quot;], Rails.root.join('tmp'))
          tempfile.binmode
          tempfile.write pdf_string
          tempfile.close
  end
  </code></pre>
  <p>I am getting following error</p>
  <pre><code>*** ActionView::MissingTemplate Exception: Missing template /#&lt;File:0x00007f236ccf4fe0&gt; with {:locale=&gt;[:en], :formats=&gt;[: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=&gt;[], :handlers=&gt;[:raw, :erb, :html, :builder, :ruby, :haml]}. Searched in:
  </code></pre>
  <p>EDIT</p>
  <p>I have tried following as well</p>
  <pre><code>url = Rails.application.routes.url_helpers.url_for(@attachment.attachment.attachment)
  data = open(&quot;#{url}&quot;).read
  @base64_encoded = Base64.encode64(data)
  file = Tempfile.new([&quot;#{@attachment&amp;.attachment_blob&amp;.filename.to_s.sub(/\..*/, '')}&quot;, &quot;.pdf&quot;], Rails.root.join('tmp'))
  File.open(file, 'wb') do |f|
   f.binmode
   f.write Base64.decode64(@base64_encoded)
   f.rewind
  end
  </code></pre>
  <p>but it's not working I got following error<br />
  &quot;File type PNG image (image/png) is not supported&quot;
  while I open the pdf file from /tmp folder, and got a blank file</p>
  <p>Edit</p>
  <pre><code>    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([&quot;#{@attachment&amp;.attachment_blob&amp;.filename.to_s.sub(/\..*/, '')}&quot;, &quot;.pdf&quot;], 
      Rails.root.join('tmp'))
      tempfile.binmode
      tempfile.write pdf_string
      tempfile.close
  </code></pre>
  <p>This code works fine with the image file like jpg/png and able to generate pdf with the image.</p>
  <p>but how to achieve that with the windows file(docx/xlsx) or csv/text file?</p>
  <p>I try to open those files by the following code but get the Readtimeout error for a small file.</p>
  <pre><code>    url = Rails.application.routes.url_helpers.url_for(@attachment.attachment.attachment)
      data = open(&quot;#{url}&quot;).read
  </code></pre>