0

I'm creating a visuaforce page as pdf to display some account and opportunity fields in pdf. But when I try to read the pdf and add it into attachement I find that is blank. Please could you help me to find a solution or to create pdf in js because I'm working with lwc. After reading the pdf in apex method I call it in js.

apex method

PageReference page = new PageReference('/apex/PDFContract');
// add our record id
page.getParameters().put('Id', recordId);
System.debug('Before AccountPDFController::::::::::::::::::::');
AccountPDFController a=new AccountPDFController(tempId,OppId);
Attachment att=new Attachment();
att.Body=page.getContentAsPDF();
System.debug('getContentAsPDF:::::'+page.getContentAsPDF());
att.ContentType = 'application/pdf';
system.debug('att.Body***************'+att.Body);
att.Name='Contract2 ' + System.now().format('yyyy_MM_dd_hh_mm_ss')+'.pdf';
system.debug('Contract***************'+att.Name);

att.parentId=recordId;
insert att;

visualforce page

 <apex:page controller="PDFController" renderAs="pdf">
    <h1>The Contract</h1>
      <apex:pageblock mode="detail" title="Account Fields">
        <apex:pageblocktable cellpadding="3"  rules="rows"
          value="{!accFields.accountsLabels}" var="pr" 
              headerclass="eventTable" footerclass="eventTable"
           styleclass="eventTable">
     <apex:repeat value="{!pr}" var="key">
     <apex:column >
     <apex:outputText value="{!pr[key]+': ' }"/>
     <apex:outputText value="{!accFields.accountsValues[0][key]}"/>
     <!-- you can add more fields -->
     </apex:column>
     </apex:repeat>
     </apex:pageblocktable>
     </apex:pageblock>
     <apex:pageblock mode="detail" title="Opportunity Fields">
     <apex:pageblocktable cellpadding="3"  rules="rows"
      value="{!oppFields.opportunitiesLabels}" var="pr" 
      headerclass="eventTable" footerclass="eventTable"
      styleclass="eventTable">
     <apex:repeat value="{!pr}" var="key">
     <apex:column >
     <apex:outputText value="{!pr[key]+': ' }"/>
     <apex:outputText value="{!oppFields.opportunitiesValues[0][key]}"/>
     <!-- you can add more fields -->
     </apex:column>
   </apex:repeat>
 </apex:pageblocktable>
</apex:pageblock>

  • 2
    you should share the code, not the image! – Klecool Jun 9 at 8:57
  • Do you want to display the pdf generated in the lightning environment, not a classic environment? – Klecool Jun 9 at 9:02
  • Hey @Klecool . I want to add the pdf as attachment in lightning environment. – sos Jun 9 at 9:49
  • In the debug log are you seeing results for getContentAsPDF ? – Kiran Machhewar Jun 9 at 16:47

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.