function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TehNrdTehNrd 

How to define PDF filename?

When rendering a page as a PDF the filename of the PDF is the name of the page which is not a good thing. The problem with this is that the name is not unique and can cause confusion with the user.

 

I'm working on a quoting app that renders a quote as a PDF. Some broswers open the PDF embed, others automatically launch your PDF reader, and some prompt you to save or open. The problem is that if opened or saved theses files are all saved as qoute.pdf, qoute[1].pdf, quote[2].pdf, quote[3].pdf. The problem should be obvious.

 

Ideally you should be able to define the name of the generated PDF but I haven't figured out how to do this.

 

Thanks,

Jason

AltiumForceAltiumForce
I am having the same issue, anyone have a solution?
KevinLaurenceKevinLaurence

In a Visualforce email template the following code works well:

 

 

<messaging:attachment renderas="pdf" filename="{!relatedTo.name}.pdf">

 

If the name field of the related object is an autonum field, each pdf filename is guaranteed to be unique.

 

I don't know if something similar can be done for a standard Visualforce page (i.e. not a VF email template). 

Message Edited by KML123 on 04-29-2009 02:42 AM
AltiumForceAltiumForce

It doesn't work, <apex:page> does not have filename attribute.

 

I also tried  <apex:page renderAs="PDF" contentType="application/pdf#123.pdf">, doesn't work either.

jlojlo

This code generated a file called '0018000000PlLFm.xls' for me in FF3:

 

 

<apex:page standardController="Account" contentType="application/vnd.ms-excel#{!Id}.xls"> </apex:page>

 

The url for the page was: https://c.na6.visual.force.com/apex/downloadfilename?Id=0018000000PlLFm

 

Saido7Saido7

Same. No Solution yet???

 

Regards,

Sait

jeremyajohnsonjeremyajohnson
I'm also looking for the answer to this question. 
SoloSolo

Hello,

 

Folloving solution will work: <apex:page contentType="application/pdf#{!relatedTo.name}.pdf">

 

But there is an issue. Instead of generating pdf file on server side you will force SF to generate file on local machine.
It  will  take  more time and result not predictable if you use Static
Resources ...

 

The best way is to ask SF team to add filename attribute for <apex:page>.

 

 

 

 

TigerPowerTigerPower

Since filename attribute is not yet supported for apex:page, has someone already found way to solve this pdf naming problem? Would it be possilbe to take care of the pdf file name with controller extension?

Keith654Keith654

I need this feature too.

Chirag MehtaChirag Mehta

Where you guys able to figure out a way to define the name of the generated(using renderAs) PDF?

thunksalotthunksalot

bump.  i need this too.

mindrayITmindrayIT

Has anyone come up with a solution for this?

Chirag MehtaChirag Mehta
I was not able to find one.
SimbaSimba

If you add following lines of code in the Constructor of the Controller of the PDF page, it should do the trick -

 

String yourFileNameName = 'nameofFile.pdf' ;

Apexpages.currentPage().getHeaders().put( 'content-disposition', 'inline; filename=' + yourFileName );

 

Hope this helps..

mindrayITmindrayIT

Thank you for posting the suggestion but I could not get it to work.  The only affect it had was to disable the "Save" on the pdf file.  I have no VF experience so I suspect that might be the first problem ;-)

 

Here is the general layout:

public with sharing class SVMX_ServiceOrderPrintSummary {
 private String sId = System.currentPageReference().getParameters().get('id');
 private static SVMXC__Service_Order__c WOHeader; 
 ....
  public SVMX_ServiceOrderPrintSummary()
  {
        populateWOHeader();
        populateWorkOrderLines();
     // inserted your code here
     String yourFileName = 'nameofFile.pdf' ;
     Apexpages.currentPage().getHeaders().put( 'content-disposition', 'inline; filename=' + yourFileName );
  }

....
}

Desert DudeDesert Dude

This code works with a slight modification (typo).  Line 2 of your code should read + yourFileNameName instead of + yourFileName. So in my case I did the following:

 

 

==============================

public class PurchaseReqPDFExt {

    

    private Id appId;

    

    

        public PurchaseReqPDFExt(ApexPages.StandardController c) {

            this.appId = (Id) c.getId();

 

          // I want to append the Name field as a suffix to PurchaseOrder filename so it will read Req-004PurchaseOrder.pdf (or whatever)

            // Below I make the SQL query and get the fields I want from the Purchase_Req__c object and it will be called Request variable

            // Then I insert the variable.field append it to my original filename and when I render the VF page and save, it appends the Name field to PurchaseOrder.pdf

 

            String yourFileNameName = Request.Name + 'PurchaseOrder.pdf' ;

            Apexpages.currentPage().getHeaders().put( 'content-disposition', 'inline; filename=' + yourFileNameName );

        }

 

        

        public Purchase_Req__c Request {

            get {                    

                return [SELECT Name, Id, Status__c, Purchase_Amount__c, Actual_Amount__c, LastModifiedDate, CreatedDate,

                        Requester_Name__c, Requester_Name__r.Name, Requestor_email__c, Description__c, Last_Name__c,

                        Justification__c, First_Name__c, DivisionAcc__c, DivisionAcc__r.Name, Date__c,

                        Address__c, Phone__c                                                                   

                        FROM Purchase_Req__c 

                        Where Id =:this.appId];

                        

            }

        }

 

mindrayITmindrayIT

I decided to make it even shorter and hard code the file name.  Here is the code and then the results:

public with sharing class SVMX_ServiceOrderPrintSummary {
 private String sId = System.currentPageReference().getParameters().get('id'); 
 private static SVMXC__Service_Order__c WOHeader;
 private static List<SVMXC__Service_Order_Line__c> woLine = new List<SVMXC__Service_Order_Line__c>();
 List<SVMXC__Service_Order_Line__c> partsList = new List<SVMXC__Service_Order_Line__c>();
 List<SVMXC__Service_Order_Line__c> laborList = new List<SVMXC__Service_Order_Line__c>();
 List<SVMXC__Service_Order_Line__c> expList = new List<SVMXC__Service_Order_Line__c>();  
  
  public SVMX_ServiceOrderPrintSummary()
  {

        populateWOHeader();
        populateWorkOrderLines();

Apexpages.currentPage().getHeaders().put( 'content-disposition', 'inline; filename=mytest.pdf' );

  }

 

When I go to save the PDF, it defaults to the VF page name (as usual) which is "SVMX_VF_ServiceOrderPrintSummary.pdf"

 

Thanks for your help though!

SimbaSimba

 

So I doublechecked after your response and looks like that code works on Google Chrome, but does not work on Internet Explorer or Firefox. Will need to do more research to see if there is any other attribute that we can set as content-disposition instead of 'inline' to make it work for all the browsers.

 

Please let me know if you find a solution for IE

 

Thanks

mindrayITmindrayIT

OK - will do and thanks again...

APathakAPathak

Hi,

Try using attachment instead of inline. This works in IE too. However the behavior is changed in the manner that Save/Open dialog appears instead of pdf rendering in browser . 

 

String yourFileName = 'MyFile.pdf'
Apexpages.currentPage().getHeaders().put( 'content-disposition', 'attachment; filename=;' + yourFileName );

 

Ramakrishnan AyyanarRamakrishnan Ayyanar

APathak,
String yourFileName = 'MyFile.pdf' ;
Apexpages.currentPage().getHeaders().put( 'content-disposition', 'attachment; filename=;' + yourFileName );

This is also not working.It's downaloaded as pdf but if i try open file file format nort support something this is kind of error we got it.


 
Andrew FrederickAndrew Frederick

Here's my solution that definitely worked but bare in mind I already overwrote the GUI with a view state VF page...

-first make 2 VF pages (One for View state (ViewObject) and one for Print state (PrintObject))
-make an extension controller that BOTH will reference so that whatever you use to populate stuff in ViewObject can be populated PrintObject
-put whatever content you need on each and format the layout for the print state however you'd like (one of the upsides of doing it this way)
-put the renderAs="pdf" in page tag of PrintObject
-put this commandlink in ViewObject
        <apex:commandLink action="{!print}" target="blank"> Print </apex:commandLink>

-put this is in the Extension controller

        //Property set in Controller
        public Object__c record {get;set;}   

         //Print method for view state commandlink
        public PageReference print(){
      
                       Id objId = this.record.Id;
                       String objName = this.record.Name;

                       PageReference printPage = new PageReference('/apex/PrintObject');
        
                       printPage.getParameters().put('id', objId);
                       printPage.getHeaders().put('content-disposition', 'attachement; filename='+objName+'.pdf');
        
                      return printPage;
    }


Not only do you have a printable PDF of whatever record the resulting filname for something such as custom Sales Order object if the name is SO100100 will be SO100100.pdf!
 

Nikolay MitjulNikolay Mitjul
@simba

Simba (https://dfc-org-production.force.com/forums/ForumsProfile?communityId=09aF00000004HMG&userId=005F0000003FZ0k&showHeader=false" id="ext-gen52" style="font-family:DSCDefaultFontRegular; color:#777777; text-decoration:none; font-weight:700)
If you add following lines of code in the Constructor of the Controller of the PDF page, it should do the trick -
 
String yourFileNameName = 'nameofFile.pdf' ;
Apexpages.currentPage().getHeaders().put( 'content-disposition''inline; filename=' + yourFileName );

Thank you. It works with extension in my case.

public class theNameOfExtansion {
    public theNameOfExtansion (ApexPages.StandardController stdController){
        
        String downloadingFileName = 'theNameOfDocument.pdf';
        Apexpages.currentPage().getHeaders().put( 'content-disposition', 'inline; filename=' + downloadingFileName );
    }