I have a Visualforce page that is rendered as PDF. It works from the browser:

enter image description here

I have a method that attaches a Visualforce PDF pageReference to a record:

public static Id attach_PDF_To_Record(PageReference pageRef, String record_param_name, Id record_param_id, String content_parent_param_name, Id content_parent_param_id, String file_name){
        PageReference pdf = pageRef;
        // simulate the PDF with content_parent_params
        pdf.getParameters().put((content_parent_param_name.equals('')? 'id' : content_parent_param_name), content_parent_param_id);

        Attachment attach = new Attachment();

        Blob body;

        try {
            // get PDF data
            body = pdf.getContent();
        catch (VisualforceException e) {
            body = Blob.valueOf(e + ' MORE INFO: ' + e.getStackTraceString());

        attach.Body = body;
        attach.Name = file_name + '.pdf';
        attach.IsPrivate = false;
        attach.ParentId = record_param_id; // attach PDF to the specified record 

        insert attach;

        return attach.Id;

After attaching the PDF to the record, when I try to 'View File' via Salesforce UI, I get 'This PDF document might not be displayed correctly. Open with a different viewer', although the attachment has a size of 367KB:

enter image description here

The page source is super simple at this point:

<apex:page renderAs="pdf" expires="1" sidebar="false" showHeader="false" Controller="My_Controller">

UPDATE 2: Like Keith suggested, I changed the page source to the following and still no luck:

<apex:page renderAs="pdf" Controller="My_Controller">

Can someone tell what I could be missing here? Thank you.

UPDATE 1: Changed the default PDF application in Firefox and still get an error while viewing the file:

enter image description here

  • Based on your screen shot looking like Firefox and e.g. forums.mozillazine.org/viewtopic.php?f=38&t=2724977, this is probably more to do with your browser setup than with Salesforce. – Keith C May 20 '15 at 18:01
  • I changed the default application for PDFs to Adobe Reader in Firefox as opposed to native Firefox viewer, still no luck. Interestingly, the same method works for a different visualforce PDF in the same instance in the same browser. Something is not right with this one. Any other thoughts? – Lightning Evangelist May 20 '15 at 21:24
  • Yeah I see what you mean. I have Visualforce generated PDF that works fine. Have you tried varying the content e.g. <div>TEST</div> or leaving the content blank? I've never set expires on a PDF. – Keith C May 21 '15 at 8:38
  • I tried it... see UPDATE 2 in OP. No luck yet... very weird. – Lightning Evangelist May 21 '15 at 12:49
  • Can someone please take a look at this? Thank you. – Lightning Evangelist May 29 '15 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.