2

I successfully made a connection to version 1 using the below code:

    <cfhttp method="post" 
        url="http://do.convertapi.com/Word2Pdf" 
        result="convertAttempt" 
        path="#arguments.path#" 
        file="#arguments.fileToDestination#"
    >
       <cfhttpparam type="formfield" name="ApiKey" value="xxxxxxx" >
       <cfhttpparam type="file" file="#arguments.path#/#arguments.fileToConvert#" name="File" >
   </cfhttp>

Below is the code I am trying to use for version 2. It writes a file to the correct folder, but it's not a readable PDF. I think it has something to do with base64, but not sure. Anyway, hoping there's another ColdFusion user out there to help me out. Then, we hopefully get code samples on the convertAPI site to help others.

<cfhttp method="post" 
    url="http://v2.convertapi.com/docx/to/pdf?Secret=mysecret" 
    result="convertAttempt"
    path="#arguments.path#" 
    file="#arguments.fileToDestination#"
>   

    <cfhttpparam type="file" file="#arguments.path##arguments.fileToConvert#" name="File" >
</cfhttp>
SOS
  • 6,430
  • 2
  • 11
  • 29
user1431633
  • 658
  • 2
  • 15
  • 34
  • 1
    What does the API return? If it's a base64 string, you must decode it into binary before saving to a file. That can't be done in one step. Get rid of the "file" and "path" attributes. After the cfhttp call, check the status code. If successful, convert the base64 response string into binary and save the binary to a file. Something like `FileWrite("#arguments.path#\#arguments.fileToDestination#", BinaryDecode(convertAttempt.filecontent, "base64"))`. ... On the other hand, if the API returns binary, try adding `getAsBinary=true' to the cfhttp call. – SOS Apr 04 '18 at 02:38
  • That was it. Not sure how I make a comment an answer... – user1431633 Apr 04 '18 at 03:13
  • Glad you figured it out. Though normally, you'd want to ask the person who provided the solution to post their comment as an answer (if it contributed to solving the problem) ;-) – SOS Apr 04 '18 at 17:03
  • Yes, please put your suggestion as an answer and I'll mark it. – user1431633 Apr 04 '18 at 20:13
  • Actually, reading the responses it was more involved than just binaryDecode, so your answer is better... Though adding an an explanation of what it does would make it more helpful to the next guy that has the same problem :) https://stackoverflow.com/suggested-edits/3649755 – SOS Apr 04 '18 at 21:16

2 Answers2

2

By default ConvertAPI version 2 returns JSON. You need to decode the file using a Base64 decoder.

To save response time and bandwidth, better add the accept=application/octet-stream header to the request, to get an instant binary response without any decoding.

Community
  • 1
  • 1
Tomas
  • 17,551
  • 43
  • 152
  • 257
2

Using the suggestions in the comments and Tomas's answer, here is my final code. It first deserializes the response from JSON. Then decodes the pdf from base64 into binary. Finally, saves the binary pdf file to disk.

<cfhttp method="post" url="http://v2.convertapi.com/docx/to/pdf?Secret=your-secret" result="convertAttempt">    
   <cfhttpparam type="file" file="#arguments.path##arguments.fileToConvert#" name="File" >
</cfhttp>

<cfset FileResult = deserializeJSON(convertAttempt.FileContent) />

<cfif isDefined("fileResult.Code")>
    <!--- Failed --->
<cfelse>
    <cfset FileWrite("#arguments.path##arguments.fileToDestination#", BinaryDecode(FileResult.Files[1].FileData, "base64"))>
</cfif>
user1431633
  • 658
  • 2
  • 15
  • 34
  • 1
    Glad you figured it out! One recommendation. In the real code, be sure to check the http status code, before attempting to parse the response. If the status code isn't "200 OK", you know something went wrong and the parsing will fail, so do some error handling. – SOS Apr 04 '18 at 17:15
  • RE: "couldn't get this piece working" Try changing the header "name" to `name="Accept"`. Also, the `cfset` statements should be inside the `cfif` so the only execute if the http status is "200 OK" (not just 200). `... save pdf ... ... do stuff on error ` – SOS Apr 04 '18 at 23:52
  • Errors how, does the http call fail or the code after that? The "Accept" header tells the other end what type of response you want to receive. "application/octet-stream" means you'll accept binary (what you originally wanted). IF it works, you'd get back a binary pdf. So you could go back to your original code. No deserialize/decode needed because the response would just contain the pdf. At least that's what I got from Tomas' answer... – SOS Apr 05 '18 at 15:46
  • Everything is working well so I got rid of the stream code. – user1431633 Apr 05 '18 at 17:07
  • Okay, good. Just explaining what (I think) Tomas was getting at. Glad everything is working! – SOS Apr 05 '18 at 17:24