Clark
Clark

Reputation: 63

Converting Word Doc to PDF in Coldfusion

Working on a page that displays the Standard Operating Procedures (SOP) and allows a non-Admin to download the SOP in a PDF file. Clients decided they didn't want admins to be limited to uploading just PDF files, and they want the option to upload .doc and .docx files. I need the download link to produce a PDF though.

Right now uploading either a .doc/.docx or a .pdf will display as I want it to using the Google Viewer. But when I attempt to download the test file it cant be opened if the files uploaded was a .doc/.docx. I've looked over this, and I'm sure I'm missing something stupid.

I'm using cfdocumnet as was suggested on another question.

Download link:

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

Check for Admin (variable is created elsewhere) and form to upload file:

<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>

And on a side note, because I want the downloadable .pdf to be the given name "SOP.pdf" is there a way I can delete the user-uploaded file after renaming it and converting it? Just so there aren't 30 different outdated SOP documents on the server.

Upvotes: 0

Views: 982

Answers (1)

SOS
SOS

Reputation: 6550

The form upload code looks wrong. Due to the cfparam, it's probably trying run the upload/conversion before the form is even submitted. Remove the cfparam and use structKeyExists() to verify the file field was submitted before trying to process it.

Try a simplified example first, with only the form and upload code (no error handling).

<!--- 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> 

Also, though a bit dated, some of these tips on securing file uploads are still valid (such as not uploading files to the web root):

Upvotes: 2

Related Questions