2

EDIT:

I have no problem viewing the page and it does display the fields but my problem is when you generate content as pdf it does not show the visualforce page content in the PDF

I have a standard page and in that standard page layout I have added Custom Visualforce page and added a Detail Page Button at the top of the page layout. when the user clicks on the page it execute the code below to generate the PDF and attach to notes/attachments, all is good so far.

PageReference pagePdf = new PageReference('/'+parentId);         
pagePdf.getParameters().put('id', parentId); 
Attachment a = new Attachment();
a.Body = pagePdf.getContentAsPDF();
a.ParentID = parentId; 
a.Name = 'Attach.pdf';
insert a; 

The issue I see is that it does not generate visualforce page fields at all.

How do I solve this issue? any thoughts?

4
  • You have a problem with a visual force page displaying fields - you have been around long enough to know what I am going to say next.......
    – Eric
    Commented Apr 14, 2017 at 0:58
  • Can you post visualforce page in this question Commented Apr 14, 2017 at 0:59
  • @Eric: I have no problem viewing the page and it does display the fields but my problem is when you generate content as pdf it does not show the visualforce page.
    – Nick
    Commented Apr 14, 2017 at 1:02
  • And the reason for it is most likely due to how your mark is and what you are using. If I say that I can debug jsonString but when I parse it I get an error and I do not provide the string could you answer it? Same concept
    – Eric
    Commented Apr 14, 2017 at 1:04

1 Answer 1

1

I am not sure whether it will solve your problem, but can you try using getContent() instead of getContentAsPdf().

PageReference pRef = Page.PagePDF;                 

Attachment attach = new Attachment();
Blob body;
if(Test.isRunningTest())
{
 body = Blob.valueOf('pdfBody');
}
else
{
 body = pRef.getContent();   
}         
attach.Body = body;
attach.Name = 'Attach.pdf';
attach.IsPrivate = false;
attach.ParentId = parentId;

insert attach;
pRef = new PageReference('/' + parentId);            
pRef.setRedirect(false); 
return pRef;

<apex:page> attibutes like this:

<apex:page 
    standardController = "StandardControllerName" 
    extensions="CustomExtension"  
    applyBodyTag="false" renderAs="pdf" 
    standardStylesheets="false" 
    applyHtmlTag="false" 
showHeader="false">

Also, I could think of issue in Transnational Scope.

All changes are committed to the database only after all operations in the transaction finish executing

Refer this answer Fields missing using getContent call for Visualforce rendered as PDF

1
  • Aw, Test.isRunningTest(). Tsk tsk!
    – Adrian Larson
    Commented Sep 1, 2017 at 12:19

You must log in to answer this question.

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