Convert DOCX to PDF file stream in Ruby using Aspose.Words REST API

I need AsposeWords to do Docx to PDF conversion. Now, following one of the Ruby cloud conversion examples (from the Ruby SDK), I was able to successfully do a conversion.

class Document

  include AsposeWordsCloud

  def initialize
    AsposeWordsCloud.configure do |config|
      config.api_key['api_key'] = "<foobar>"
      config.api_key['app_sid'] = "<foobar>"
    end
    @words_api = WordsApi.new
  end

  def put_convert_document
    format = 'pdf'

    request = PutConvertDocumentRequest.new(File.open('parent/child/file.docx', "r"), format)
    result = @words_api.put_convert_document request
  end
end

However, after conversion was completed, the file was downloaded to my local drive in a temp/ directory. As this will be running a serverless environment (AWS Lambda), I won’t have access to the file system.

Is there a way to i) stream the bytes to my runtime or ii) is there an integration with AWS S3, that lets me store conversion results in a specific bucket and location?

I looked around the source code (here, here and here) and message board (here and here) and saw some similar questions, but not an answer for this. Thanks for any insights.

Thanks for your inquiry. I am looking into your requirements and will update you shortly.

@tim.washington

It seems the PutConvertDocumentRequest method of Ruby SDK is not working as expected, it closes the stream after writing file to the temp folder. We have logged a ticket WORDSCLOUD-783 in our issue tracking system for further investigation and resolution.

Yes, you can save the conversion result to storage. You need to use output and storage folder parameter as follows. Please note Aspose.Words Cloud uses Aspose storage by default. However, if you want to use any other third party storage of your choice, then you need to configure it with Aspose and pass it the request accordingly.

    def put_convert_document
    filename = 'TestDocx.docx'
    format = 'pdf'
    storage = nil	
    output='output/test.pdf'
        
    request = PutConvertDocumentRequest.new(File.open(FILE_PATH  + filename, "r"), format,storage,output)	
    result = @words_api.put_convert_document request
    end

@tilal.ahmad Thanks for that. I’ll look forward to any resolution you have to item 1.

@tim.washington

Sure, we will notify you as soon as the issue is resolved.

@tim.washington

Thanks for your patience. You can use following code. In provided example variable f is a variable of File class and you can use it to do any manipulations with converted file.

format = 'pdf'
filename = 'test_multi_pages.docx'
request = ConvertDocumentRequest.new(File.open(local_common_folder  + filename, "rb"), format)
result = @words_api.convert_document request
f = File.open(result.path, 'r')