2

I'm currently working on a Rails 5.2 application using the combine pdf gem. I'm trying to merge two PDF files but somehow I'm unable to load files from the public directory.

In the controller I have the following method:

def pdf_download
  pdf = CombinePDF.new
  pdf << CombinePDF.load("#{Rails.root}/public/pdfs/1.pdf") 
  pdf << CombinePDF.load("#{Rails.root}/public/pdfs/1.pdf")
  pdf.save "combined.pdf"

  send_data combined_file.to_pdf, filename: "combined.pdf", type: "application/pdf"
end

I have tried many posts on StackOverflow without success such as using Rails.root. But I still get the same error:

Errno::ENOENT (No such file or directory @ rb_sysopen - app/public/pdfs/1.pdf):

Is there any additional configuration I have to do to load files from public? and if these PDF shouldn't be in public where should I store them?

Steven Aguilar
  • 3,107
  • 5
  • 39
  • 89
  • 2
    Why does one example start with `#{Rails.root}/` and the other with `./`? What is your actual code? Where do the files actually exist? – spickermann Jul 24 '18 at 19:09
  • The code you've written is clearly different to the code in the error message. Can you please verify which version of the code you're running, and show the corresponding error (if there is one!) – Tom Lord Jul 24 '18 at 19:16
  • 1
    if these PDF shouldn't be in public where should I store them? Answer is you can upload your file on amazon s3 and can use active storage – Bodh1004 Jul 24 '18 at 19:29
  • @tom-lord my apologies by mistake I uploaded the wrong image. However, I got passed this issue but wondering if theres a more optimal solution. Please feel free to comment on the answer. – Steven Aguilar Jul 24 '18 at 20:55

2 Answers2

1

This fixed it for me:

  def pdf_download
    pdf = CombinePDF.new
    pdf << CombinePDF.load(Rails.root.join("public", "pdfs/1.pdf").to_s)
    pdf << CombinePDF.load(Rails.root.join("public", "pdfs/2.pdf").to_s)
    # pdf.save "combined.pdf"

    send_data pdf.to_pdf, filename: "combined.pdf", type: "application/pdf"
  end
Steven Aguilar
  • 3,107
  • 5
  • 39
  • 89
1

This fixed it for me:

pdf << CombinePDF.load("./public/pdfs/1.pdf")
PepeGomez
  • 11
  • 1