Creating PDF documents dynamically using Umbraco and XSL-FO part 2

Since my last post I have made a few modifications to the PDF generation, the main one being that the files are now dynamically renamed so that they reflect the name of the case study instead of all being called PDF.PDF which was not a very helpful filename, I just wanted to get something live last week, so decided that something was better than nothing :)

The issue with the filenames comes down to the way that the PDF's are being generated by using an alternative template in Umbraco, this means that all you need to do is add " /pdf " to the end of a case study URL and it will create a PDF version of the case study. The down side is that your browser will merrily download the file and save it as PDF.PDF because that is the name of the last part of the URL.

What you need to do is set the content-disposition header to be equal to the name you would like the file use, Darren Ferguson mentioned this on the Change the name of the PDF forum post.

We have used the same technique for downloading dynamically generated excel files, so I thought it would be useful to create a small macro to set both this header and also to set the caching headers to prevent any caching issues, I think in the past we have experienced all possible issues, including various issues where IE behaves differently to other browsers when you are using SSL and so the below code should work in all situations!

The template for the PDF alternative template is very simple:

<%@MasterLanguage="C#"MasterPageFile="~/umbraco/masterpages/default.master"AutoEventWireup="true" %>


The following code snippet is the XSLT macro that simply creates our file name and then passes the file name into the helper function:

<xsl:templatematch="/"><xsl:variablename="fileName"><xsl:text>Vizioz_</xsl:text><xsl:value-ofselect="$currentPage/@nodeName"/><xsl:text>_case_study.pdf</xsl:text></xsl:variable><xsl:value-ofselect="Vizioz.Helper:AddDocumentDownloadHeaders('application/pdf', $fileName)"/></xsl:template>

And the following code is the helper function that clears the current response and adds all the appropriate headers:

publicstaticvoidAddDocumentDownloadHeaders(string contentType,string fileName){HttpResponse response =HttpContext.Current.Response;HttpRequest request =HttpContext.Current.Request;
 response.ClearHeaders();if(request.IsSecureConnection& request.Browser.Browser=="IE"){// Don't use the caching headers if the browser is IE and it's a secure connection// see:}else{// force not using the cache

 response.AppendHeader("Keep-Alive","timeout=3, max=993");
 response.AddHeader("content-disposition","attachment; filename=\""+ fileName +"\"");
 response.ContentType= contentType;}

I will write another blog soon with some more details about XSL-FO and how to create the PDF's dynamically.

Please do re-tweet if you find this interest :)


Chris Houston

Chris Houston

Chris is the founder of Vizioz and has been working in the field of website development for over 20 years. Chris has been working with Umbraco since 1998 and now spends his time running both Vizioz and QV Offices.

comments powered by Disqus