Can I upload a Prawn generated PDF to Cloudinary on Rails? - ruby-on-rails

I am generating a PDF file using the Prawn gem and trying to upload the file to Cloudinary, but I'm getting the error below.
"#<NoMethodError: undefined method `match' for #<Prawn::Document:0x00007f80b51545e0>>"
The code in Rails that I am using is as follows. It generates a file in the root directory but giving the aforementioned error:
pdf = Prawn::Document.new
pdf.text "Hello World"
pdf.render_file "example.pdf"
result = Cloudinary::Uploader.upload(pdf, options = {})
I have been able to upload the same file manually to Cloudinary. Is this programmatically possible within Rails? I cannot find any resources online one way or the other.
Cloudinary is working in my project just fine for image files.
***** UPDATE *****
The upload worked with the following code. It creates a pdf file in a directory, uploads it to Cloudinary, then deletes it:
pdf = Prawn::Document.new
pdf.text "Hello World"
#creates file in directory
path = "public/system/temp_files/pdf_files/example.pdf"
pdf.render_file(path)
result = Cloudinary::Uploader.upload(path)
#deletes file in directory
File.delete(path)

Related

Rails: Rename a Binary File uploaded to API

I've seen a lot of threads that open up a file from a directory and rename it. But in my case, the user is uploading a file to a POST endpoint. I'd like to rename that file before uploading to my blob storage.
Here's what I have so far:
def picture
#file = params[:file]
#new_file_name = generate__filename()
#this line didn't work
File.rename(#file, #new_file_name + File.extname(#file))
# begin upload here
end
I'm testing this by selecting the form-data value as file in Postman. How do I rename this file?

Upload Wicked generated PDF to AWS S3 in Rails 5

I am generating invoices as PDFs and want to upload them directly to S3.
I am using Wicked-PDF and the official AWS SDK.
gem 'wicked_pdf'
gem 'aws-sdk-s3', '~> 1'
Now I create the PDF:
pdf = render_to_string pdf: "some_file_name", template: "invoices/download", encoding: "UTF-8"
And want to upload it:
s3 = Aws::S3::Resource.new(region: ENV['AWS_REGION'])
obj = s3.bucket('bucket-development').object('Filename')
obj.upload_file(pdf)
The error I get:
ArgumentError: string contains null byte
If I store the PDF first to a defined path and use the save_path it works:
save_path = Rails.root.join('public','filename.pdf')
File.open(save_path, 'wb') do |file|
file << pdf
end
But I would like to upload he temporary PDF directly to S3 without saving the PDF first to my public folder.
The upload_file method from AWS S3 SDK is working with files - see the method's description.
For uploading an object from memory, you should use the put method - see the method's description in the 2nd way of uploading on this page

Converting a pdf to jpeg using Rmagick

I am trying to convert a pdf to a jpeg image using Rmagick. I am running into some trouble with the following code:
pdf_link = "https://staging.shurpa.com/deliveries/BtrPsIxl/label.pdf"
file = Tempfile.new(['order', '.jpeg'])
p pdf_link
p file.path
im = ImageList.new(pdf_link)
puts "SUPP"
im.write(file.path.to_s)
I recieve this error:
"https://staging.shurpa.com/deliveries/BtrPsIxl/label.pdf"
"/var/folders/qm/yk_w5d9545j_6wqk6100dhjm0000gq/T/order20170706-43294-
15myct1.png"
Magick::ImageMagickError: unable to open file `/var/folders/qm/yk_w5d9545j_6wqk6100dhjm0000gq/T/magick-43294MCNyzIu4Oenn': No such file or directory # error/constitute.c/ReadImage/544from/Users/timnaughton/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rmagick-2.16.0/lib/rmagick_internal.rb:1616:in `read'
However the code works perfectly fine when I feed it this pdf_string:
"https://shippo-delivery-east.s3.amazonaws.com/b2a3e1cd070748cd80b492aa421832a3.pdf?Signature=nf6woycGiOydPI8eSnLcq3r0tEc%3D&Expires=1530816480&AWSAccessKeyId=AKIAJGLCC5MYLLWIG42A"
There appears to have been an issue with the service that was providing the pdf to me. The pdf was recently changed to a secure state and I needed to utilize access keys. This resulted in rmagick not being able to access the image file and returning the stated error.

Serving excel sheet (.xls) to browser for download using rails & angular

I am using spreadsheet gem to generate .xls file. After writing it to a file, I am trying to send to client browser for download.
Below is the code in rails
workbook = Spreadsheet::Workbook.new
# Constructed the data
file = "/path/to/file/sheet.xls"
workbook.write file
send_file file
This file when opened contains expected data in ideal format.
Below is the code in js:
CustomRestService.custom_post("report",{report_data: angular.toJson($scope.report_data)},"download_xls",{}).then (data)->
if data
hiddenElement = document.createElement('a')
angular.element(document.body).append(hiddenElement)
hiddenElement.href = 'data:attachment/xls,' + encodeURI(data)
hiddenElement.target = '_blank'
hiddenElement.download = "report.xls"
hiddenElement.click()
hiddenElement.remove()
But the file getting downloaded in browser contains junk data. I tried multiple solutions like below:
Using send_data, instead of send_file
Generated xls data and wrote to StringIO object to directly download
Constructed Blob object in js, with type as "application/vnd.ms-excel" and trying to download it.
All attempts failed, as I am missing something. All suggestions are welcome.
filename = "/path/to/file/sheet.xls"
tempfile = Tempfile.new(filename)
workbook = Spreadsheet::Workbook.new
...
workbook.write(tempfile.path)
send_file tempfile.path, :filename => filename

How to download files through a custom action in Rails Admin

How can I download a file through a custom action in Rails Admin. As the file which I'm sending is ZIP format but after getting downloaded its a nested zip which on opening creates another zip file and just goes on.
So basically this is my custom action where I'm calling my Service which will create a zip file through gem rubyzip and I'm sending the file_path of the created zip file with is on my local machine(e.g file_path as "#{Rails.root}/abc.zip")
else
# all your code that does the work
import_params = params.require(:documents)
.permit(:email)
if import_params[:email].present?
#result = Download::Forms.call(mail_id: import_params[:email])
if #result[:status]
flash.now[:notice] = 'File Downloaded'
send_file(
#result[:file_path],
filename: #result[:file_name],
type: "application/zip"
)
else
flash.now[:error] = "Failed to Download because #{#result[:error_message]}"
end
else
flash.now[:notice] = 'Please Enter Email Address'
end
#result
end
As I also send the #result in the end so that I can also try link_to method to download the file but all in vain. I'm using Rails 4.2.6
Some more information regarding the file types is that I tried a txt file and a pdf file also to check whether they are downloading properly but the same result they got downloaded but they were not proper as txt file was containing nothing and pdf can't be opened.
The files are getting downloaded with my custom action name and not from the file name which is the right naming if I download a file from any simple action.
Any help will be really appreciated.

Resources