将Word Doc转换为ColdFusion的PDF



在显示标准操作程序(SOP)的页面上工作,并允许非Admin下载PDF文件中的SOP。客户端决定他们不希望Admins仅限于上传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>

检查admin(在其他地方创建了变量),并形式上传文件:

<cfif isAdmin>
  <h3>Upload New SOP</h3>
  <cfparam name="form.fileUpload" default="">
  <cftry>
    <cffile action="upload"
        fileField="fileUpload"
        destination="#expandPath('.')#filesSOP"
        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('.')#filesSOP#fileName#"
          filename="#expandPath('.')#filesSOPSOP.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>

and旁注,因为我希望可下载的.pdf是给定的名称" sop.pdf",是否有一种方法可以在重命名并将其转换后删除用户删除文件?只是这样,服务器上没有30个不同的SOP文档。

表单上传代码看起来错误。由于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('.')#filesSOPSOP.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根部):

  • http://www.learncfinaweek.com/week1/file_uploads/
  • https://www.petefreitag.com/item/701.cfm

最新更新