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
Sascha DeinertSascha Deinert 

Create Pdf from visualforce page with button

Hello, 

I create a button at the account record to create a PDF. That works good, but now I want to pass information from the account record into my visual force. I tried it, see below, but the (example accid) account id does not appear at the pdf.

APEX CLASS
public class attachPDFToAccount2 {

private Id accId {get; set;}
Public List<Account> AccList {get; set;}
public String AName {get; set;}

public attachPDFToAccount2() {
    accId = ApexPages.currentPage().getParameters().get('id');
    AccList = [SELECT Name FROM Account WHERE Unternehmens_Id_Long__c = :accId]; 
    FOR (Account Acc : AccList) {
        AName = Acc.Name;
    }
}

public final Account a;

public attachPDFToAccount2(ApexPages.StandardController standardPageController) {
    a = (Account)standardPageController.getRecord(); 
}

public PageReference attachPDF() {
    
    PageReference pdfPage2 = Page.pdfpage2; 
    pdfPage2.getParameters().put('id', accId);
    Blob pdfBlob; 
    if (!Test.isRunningTest()) { 
       pdfBlob = pdfPage2.getContent(); 
     } else { 
     pdfBlob = Blob.valueOf('...');
     }

    Attachment attach = new Attachment(parentId = a.Id, Name = 'pdffile.pdf', body = pdfBlob); 
    insert attach;
    
    PageReference pageWhereWeWantToGo = new ApexPages.StandardController(a).view();
    pageWhereWeWantToGo.setRedirect(true); 
    return pageWhereWeWantToGo; 
}

}

ACTION PAGE
<apex:page action="{!attachPDF}" extensions="attachPDFToAccount2" standardController="Account">
    <apex:pageMessages ></apex:pageMessages>
    <apex:detail inlineEdit="true" relatedList="true"></apex:detail>
</apex:page>

RENDER PAGE
<apex:page renderAs="pdf" controller="attachPDFToAccount2">
<apex:stylesheet value="{!$Resource.pdfStyle}"/>
<center>
    <h1>Visualforce PDF Sample - {!accId}</h1>
</center>
<table align="center" style="border: 1px solid #6699CC;">
    <tr>
        <td style="background-color: #6699CC; color: #FFFFFF; font-size: 200%; padding: 10px;">NOTES</td>
        <td>
            <ul>
                <li>TEST</li>
            </ul>
        </td>
    </tr>
</table>
</apex:page>

​​​​​​Thanks,
Sascha
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sascha,

Are you still facing the issue. If yes please  confirm so I can have a look into it.

Thanks,