Search code examples
ruby-on-railsamazon-s3rails-activejobrails-activestorageruby-on-rails-5.2

How to convert word file to PDF in ROR


I am using Libreconv gem to convert word to doc but it's not working with S3

bucket = Aws::S3::Bucket.new('bucket-name')
object = bucket.object file.attachment.blob.key
path = object.presigned_url(:get)
Libreconv.convert(path, "public/test.pdf")

If I try to convert this path to PDF using Libreconv then it's give me filename too long error. I have wrriten this code under ActiveJobs. So kindly provide me solutions as per ActiveJobs.

Can someone please suggest me how can I convert word file to pdf. Here path is https://domain.s3.amazonaws.com/Bf5qPUP3znZGCHCcTWHcR5Nn?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIZ6RZ7J425ORVUYQ%2F20181206%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20181206T051240Z&X-Amz-Expires=900&X-Amz-SignedHeaders=host&X-Amz-Signature=b89c47a324b2aa423bf64dfb343e3b3c90dce9b54fa9fe1bc4efa9c248e912f9

and error I am getting is

Error: source file could not be loaded *** Errno::ENAMETOOLONG Exception: File name too long @ rb_sysopen - /tmp/Bf5qPUP3znZGCHCcTWHcR5Nn?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIZ6RZ7J425ORVUYQ%2F20181206%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20181206T051240Z&X-Amz-Expires=900&X-Amz-SignedHeaders=host&X-Amz-Signature=b89c47a324b2aa423bf64dfb343e3b3c90dce9b54fa9fe1bc4efa9c248e912f9.pd


Solution

  • following is the answer using combine pdf gem

    tape = Tape.new(file)
    result = tape.preview
    tempfile = Tempfile.new(['foo', '.pdf'])
    File.open(tempfile, 'wb') do |f|
      f.write result
    end
    path = tempfile.path
    combine_pdf(path)
    

    and for load file for S3 I have used

    object = @bucket.object object_key
    path = object.presigned_url(:get)
    response = Net::HTTP.get_response(URI.parse(path)).body