coldfusioncoldfusion-9cfdocument

Content-disposition being ignored in IE 9 and Firefox 13


I am trying to dynamically create an inline PDF that, when the user chooses to save it, prompts with my custom filename. According to the documentation, the saveasname attribute should do what I want.

(format="PDF" only) The filename that appears in the SaveAs dialog when a user saves a PDF file written to the browser.

However, what is happening in both IE 9 and in Firefox 13.0.1 is that the filename that appears in the SaveAs dialog is the same as my CF template, but with a PDF extension. (In other words, my code is in makepdf.cfm and the SaveAs prompts me to save makepdf.pdf.) In Chrome, however, it works perfectly. (All on Windows 7.)

Here is my code to create the PDF:

<cfdocument format="pdf" bookmark="true" saveasname="MyReport.pdf">

If I explicitly declare the content disposition and content type, like so

<cfheader name="Content-Disposition" value="inline; filename=MyReport.pdf">
<cfcontent type="application/x-pdf">
<cfdocument format="pdf" bookmark="true" saveasname="MyReport.pdf">

If I just rely on the header

<cfheader name="Content-Disposition" value="inline; filename=MyReport.pdf">
<cfcontent type="application/x-pdf">
<cfdocument format="pdf" bookmark="true">

I get the same behavior as the first snippet of code.

I know how to get the browser to prompt for download rather than displaying inline, and everything works as expected then, but that's not the desired behavior.

I need to use times and dates in filenames and the end users are not savvy enough to keep from overwriting their files (should they choose to save them).

Is there something I'm missing that will get IE and Firefox to do what they're supposed to? What other browsers are going to do this? Mobile Safari?


Solution

  • The problem seems to be that "filename=xxx" was really intended for the "attachment" disposition, and not all the browser PDF plugins recognise it as a mechanism for specifying the inline "save as", as you've discovered.

    A different approach to getting them all to use your preferred filename would be to manipulate the URL using web server rewrite rules. As a simple example you'd have your script for generating the pdfs and serving them inline: pdf.cfm

    <cfheader name="Content-Disposition" value="inline">
    <cfdocument format="PDF" mimetype="application/pdf">Test</cfdocument>
    

    Then create a re-write rule which matches URLs in the form /pdf/myfilename and passes them to pdf.cfm. On IIS7 this might be:

    <rule name="Inline PDF SaveAs" stopProcessing="true">
        <match url="^/pdf/[\w-]+$" ignoreCase="true" />
        <action type="Rewrite" url="/pdf.cfm" appendQueryString="false" />
    </rule>
    

    This will match filenames containing alphanumeric, underscore and hyphen characters only. You wouldn't want to allow spaces, or invalid filename characters.

    When you access /pdf/myreport the PDF will be displayed inline by the plugin, and when you save it, the default filename will be myreport.pdf.

    If you're using a framework which supports Search Engine Safe URLs or "routes", you could do the same without needing web server rewrites.

    UPDATE: In fact you don't need to use URL rewriting: simply append a forward slash and then the desired filename to the CF script URL, e.g.

    /pdf.cfm/myreport

    The plugin will use whatever comes after the final slash as the "Save As..." name.