在Coldfusion中将Word Doc转换为PDF

时间:2019-02-22 20:06:10

标签: coldfusion

在显示标准操作程序(SOP)的页面上工作,并允许非管理员以PDF文件下载SOP。客户决定不希望管理员只上传PDF文件,他们希望选择上传.doc和.docx文件。我需要下载链接来生成PDF。

现在上传的.doc / .docx或.pdf文件会显示为我希望使用Google Viewer时的样子。但是,当我尝试下载测试文件时,如果上传的文件是.doc / .docx,则无法打开。我已经看过这个了,我确定我想念一些愚蠢的东西。

我正在使用 cfdocumnet ,这是另一个问题的建议。

下载链接:

<cfoutput>
   <li class="active">                 
      <ahref="/files/SOP/SOP.pdf" 
        download="SOP.pdf" target="_blank">Download SOP</a>
   </li>
</cfoutput>

检查管理员(变量在其他位置创建)和表单以上传文件:

<cfif isAdmin>
  <h3>Upload New SOP</h3>
  <cfparam name="form.fileUpload" default="">
  <cftry>
    <cffile action="upload"
        fileField="fileUpload"
        destination="#expandPath('.')#\files\SOP\"
        accept="application/pdf,
             application/msword,
             application/vnd.openxmlformats-officedocument.wordprocessingml.document,
             application/x-tika-msoffice"
        nameconflict="OVERWRITE">

    <cfset fileName=CFFILE.serverfile>

    <cfdocument 
          format="PDF"
          srcfile="#expandPath('.')#\files\SOP\#fileName#"
          filename="#expandPath('.')#\files\SOP\SOP.pdf"
          overwrite="YES">
    </cfdocument>
    <p>Thank you, your file has been uploaded.</p>
    <cfoutput>#fileName#</cfoutput>

    <form enctype="multipart/form-data" method="post">
      <input type="file" name="fileUpload"/>
      <input type="submit" name="submit" value="Upload File"/>
    </form>
    <cfcatch type="any">
      <!--- file is not written to disk if error is thrown  --->
      <!--- prevent zero length files --->
      <cfif FindNoCase("No data was received in the uploaded", cfcatch.message)>
        <p>No data was received in the uploaded file.</p>
      <!--- prevent invalid file types --->
      <cfelseif FindNoCase("The MIME type or the Extension of the uploaded file", cfcatch.message)>
        <p>Invalid file type. Please upload file as a PDF or Word Doc</p>
      <!--- prevent empty form field --->
      <cfelseif FindNoCase("did not contain a file.", cfcatch.message)>
        <p>Please seclect a PDF to upload.</p>
      <!---all other errors --->
      <cfelse>
        <p>Unhandled File Upload Error</p>
        <cflog type="Error" file="#application.applicationname#_dcnsopupload_error" text="#cfcatch.Message# - #cfcatch.Detail#" />
        <cfoutput>#cfcatch.Detail#</cfoutput>
      </cfif>
    </cfcatch>
  </cftry>
</cfif>

另外,由于我希望将可下载的.pdf命名为“ SOP.pdf”,有没有一种方法可以在重命名并转换用户后删除用户上传的文件?只是服务器上没有30个不同的过时SOP文档。

1 个答案:

答案 0 :(得分:2)

表单上传代码看起来错误。由于cfparam,它可能试图在提交表单之前 进行上传/转换。删除cfparam并使用structKeyExists()来验证文件字段是否已提交,然后再尝试对其进行处理。

首先尝试一个简化的示例,仅包含表单和上载代码(无错误处理)。

<!--- If file was uploaded, process it ---> 
<cfif structKeyExists(FORM, "fileUpload")> 

    <cffile action="upload"
        fileField="fileUpload"
        destination="#GetTempDirectory()#"
        accept="application/pdf,application/msword,
        application/vnd.openxmlformats-officedocument.wordprocessingml.document,
        application/x-tika-msoffice"
        nameconflict="makeunique">

    <cfset savedFilePath = cffile.serverDirectory &"/"& cffile.serverFile>

    <!--- 
        more file validation here ... 
    --->

    <!--- convert file ---> 
    <cfdocument format="PDF"
        srcfile="#savedFilePath#"
        filename="#expandPath('.')#\files\SOP\SOP.pdf"
        overwrite="YES">
    </cfdocument>

    <!--- cleanup temp file --->
    <cfif fileExists(uploadedFile)>
        <cfset fileDelete(uploadedFile)>
    </cfif>

<!--- Otherwise, just display the form ---> 
<cfelse>
    <form enctype="multipart/form-data" method="post">
        <input type="file" name="fileUpload"/>
        <input type="submit" name="submit" value="Upload File"/>
    </form>
</cfif> 

尽管有些陈旧,但有关保护文件上传的一些技巧仍然有效(例如,不将文件上传到Web根目录):