I'm working on a Rails app to send Word files I have stored on Amazon S3 to convertapi for conversion into PDFs. I'm using the paperclip gem to manage the files, and the curb gem to make the actual request.
# model with property has_attached_file :attachment
def convert_docx_to_pdf
base = "https://do.convertapi.com/Word2Pdf"
api_key = '*****'
file = open(attachment.url)
c = Curl::Easy.new(base)
c.multipart_form_post = true
c.http_post(
Curl::PostField.file('thing[file]', file.path),
Curl::PostField.content('ApiKey', api_key)
)
end
I'm attempting to follow the documentation for curb here.
When I run this from the rails console it simply returns true
. I'd like to capture the resulting PDF.
(I've verified that this works if I upload the file manually on convertapi's test endpoint tool.)
UPDATE 09.18.15
I implemented the changes suggested by Jonas. Here's the new code:
def convert_docx_to_pdf
base = "https://do.convertapi.com/Word2Pdf"
api_key = ENV['CONVERTAPI_API_KEY']
file = open(attachment.url)
Curl::Easy.new('https://do.convertapi.com/Word2Pdf') do |curl|
curl.multipart_form_post = true
curl.http_post(Curl::PostField.content('ApiKey', api_key), Curl::PostField.file('File', file.path))
return curl.body_str
end
end
Still no luck, curl.body_str
returns just "Bad Request"
.
(file.path = /var/folders/33/nzmm899s4jg21mzljmf9557c0000gn/T/open-uri20150918-13136-11z00lk
)
Turns out the problem was simple. convertapi's Word to PDF conversion tool expects files with Word extensions. I lost the extension in the process of getting the file to S3. (file.path = /var/folders/33/nzmm899s4jg21mzljmf9557c0000gn/T/open-uri20150918-13136-11z00lk
) I was able to verify this by testing one of my actual files pulled from S3 against the convertapi web gui.
Ideally I'd make sure not to lose the extension when submitting to S3, but in the mean time the following code does the trick:
def convert_docx_to_pdf
base = "https://do.convertapi.com/Word2Pdf"
api_key = ENV['CONVERTAPI_API_KEY']
file = open(attachment.url)
local_file_path = "#{file.path}.docx"
FileUtils.cp(file.path, local_file_path) # explicitly set the extension portion of the string
Curl::Easy.new('https://do.convertapi.com/Word2Pdf') do |curl|
curl.multipart_form_post = true
binding.pry
curl.http_post(Curl::PostField.content('ApiKey', api_key), Curl::PostField.file('File', local_file_path))
# Write to PDF opened in Binary (I got better resulting PDFs this way)
f = File.open('public/foo.pdf', 'wb')
f.write(curl.body_str)
f.close
end
FileUtils.rm(local_file_path)
end