4

I would like to set a dynamic pdf filename to my visualforce page

Thé code is below, however this code still generated pdf with visualforce page generic name? (This code is Just to test to set a different static file name, bu it does not work)

I appreciate an comment or advice

<apex:page standardController="Invoice1__c" showHeader="true" renderAs="pdf" language="tr">

.

public Invoice1__c() 
{
    PageReference p =
Apexpages.currentPage().getHeaders().put('content-disposition', 'attachment; filename=AccountReport.pdf');
}

// my main code is here //

...

1 Answer 1

5

Use the contentType attribute on the VF-Page itself:

<apex:page contentType="application/pdf#SomeName{!'including dynamic components'}.pdf">

The key here is, of course, the # after the content-type.

Edit

I should have read on in my code, sorry! What you need to do is: Create a page, say testPDF and put this code in:

<apex:page contentType="application/pdf#SomeName{!'including dynamic components'}.pdf">
<apex:include pageName="testPDF_content"/></apex:page> 

Then, create a second page testPDF_content, and let the code be like this:

<apex:page renderAs="pdf">Content...</apex:page> 

Now you can call testPDF and it will be downloaded as a PDF with the filename you specified, you can try this here:

dev-sep.force.com/test/testPDF

2
  • Hi , contenttype attribute does not work for pdf files
    – irem
    Sep 14, 2014 at 17:27
  • Indeed, I should have read on in my code, sorry! What you need to do is: Create a page, say testPDF and put this code in: <apex:page contentType="application/pdf#SomeName{!'including dynamic components'}.pdf"><apex:include pageName="testPDF_content"/></apex:page>. Then, create a second page testPDF_content, and let the code be like this: <apex:page renderAs="pdf">Content...</apex:page>. Now you can call testPDF and it will be downloaded as a PDF with the filename you specified, you can try this here: dev-sep.force.com/test/testPDF
    – 0ff
    Sep 14, 2014 at 17:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .