在 Coldfusion 中将 Word 文档转换为 PDF

Converting Word Doc to PDF in Coldfusion

正在处理显示标准操作程序 (SOP) 并允许非管理员以 PDF 文件格式下载 SOP 的页面。客户认为他们不希望管理员只能上传 PDF 文件,他们希望可以选择上传 .doc 和 .docx 文件。不过我需要下载 link 来生成 PDF。

现在上传 .doc/.docx 或 .pdf 将显示我想要的使用 Google 查看器。但是,当我尝试下载测试文件时,如果上传的文件是 .doc/.docx,则无法打开。我看过这个,我确定我错过了一些愚蠢的东西。

我正在使用 cfdocumnet,正如在另一个问题上所建议的那样。

下载link:

<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 文档。

表单上传代码看起来不对。由于 cfparam,它可能在 运行 upload/conversion 之前尝试 表单甚至被提交。删除 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> 

此外,尽管有些过时,但其中一些关于保护文件上传安全的提示仍然有效(例如不将文件上传到网络根目录):