首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >convertAPI版本2文档到PDF

convertAPI版本2文档到PDF
EN

Stack Overflow用户
提问于 2018-04-04 09:02:24
回答 2查看 324关注 0票数 2

我使用以下代码成功连接到版本1:

代码语言:javascript
复制
    <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>

下面是我尝试用于version 2的代码。它将一个文件写入正确的文件夹,但它不是一个可读的PDF。我认为这与base64有关,但不确定。不管怎样,希望有另一个ColdFusion用户能帮我。然后,我们希望在convertAPI站点上获得代码示例,以帮助其他人。

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

发布于 2018-04-04 16:06:55

默认情况下,JSONVersion2返回ConvertAPI。您需要使用Base64解码器对文件进行解码。

为了节省响应时间和带宽,最好将accept=application/octet-stream头部添加到请求中,以获得即时的二进制响应,而无需任何解码。

票数 2
EN

Stack Overflow用户

发布于 2018-04-04 23:47:02

使用注释和Tomas's answer中的建议,以下是我的最终代码。它首先反序列化来自JSON的响应。然后将来自base64的pdf解码为二进制。最后,将二进制pdf文件保存到磁盘。

代码语言:javascript
复制
<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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49641167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档