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
PremanathPremanath 

Creation of PDF file

Hi Guys,

 

 I want to create a pdf of my vf page ,

 

 we can create like this way

 

pdfPage = page.product_delivery;//new PageReference('/apex/product_delivery1');
pdfPage.getParameters().put('id',oppid);
pdfPage.setRedirect(false);
Blob b=pdfPage.getContentAsPDF();

 

In my vf i am displaying some records with checkbox. When i click on Selected Button , It is displaying Selected Records.

 

I want to Create PDF for Selected Records only.

 

But it is Generating PDF for my intial VF page.. 

 

 

please suggest me..

 

 

 

prem

Best Answer chosen by Admin (Salesforce Developers) 
sguptasgupta

see if this wud help

 

<apex:pageBlockTable value="{!products}" var="p" rendered="{!hide}">
<apex:column >
<apex:inputCheckbox value="{!p.selected}"/>
</apex:column>
<apex:column value="{!p.oli.PricebookEntry.Product2.Name}"/>
<apex:column value="{!p.oli.Quantity}"/>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:commandButton value="Selected" action="{!Selected}" rerender="block2" oncomplete="sendmail"/>

 

<apex:actionfunction name="sendemail" action="{!SElectedSendemail}" />

 

<apex:pageBlockTable value="{!selectedprod}" var="p" rendered="{!show}" id="block2">
<apex:column >
<apex:inputCheckbox value="{!p.selected}"/>
</apex:column>
<apex:column value="{!p.oli.PricebookEntry.Product2.Name}"/>
<apex:column value="{!p.oli.Quantity}"/>
</apex:pageBlockTable>

</apex:pageBlockButtons>

All Answers

cmoylecmoyle

Hey prem,

Instead of generating a blob in the apex controller, have you tried using the 'renderAs' attribute for the apex:page tag in your product_delivery1 page? Something like this:

 

<apex:page renderAs="PDF">

...

</apex:page>

 

Then return the PageReference object rather than the blob in your apex method.

sguptasgupta
you can try to invoke the Selected button action using action attr of <apex:page> which will render only selected records in your pdf
PremanathPremanath

I want to Send a  mail with attached pdf

PremanathPremanath

Hi Gupta,

 

 I am selecting Records from that Vf page only...

 

Sagarika RoutSagarika Rout

You can render any page as a PDF by adding therenderAsattribute to the<apex:page>component, and specifying “pdf” as the rendering service which salesforce already provided.

 

<apex: page renderAs = "pdf">

just create a button in vf page.

 After selecting the records which you want as Pdf, then click on the button , it will convert to pdf.

for more reference use below link.

 

http://mindfiresfdcprofessionals.wordpress.com/2013/10/09/visualforce-page-that-renders-as-pdf-when-a-button-is-pushedclicked/

 

cmoylecmoyle
I also noticed a possible typo...your commented code is creating a page reference object using /apex/product_delivery1, but the actual code on that line uses page.product_delivery. Are you trying to point it to product_delivery1?
PremanathPremanath

No man product_delivery only

PremanathPremanath

Hi Rout,

 

  I want to Download that PDF file and Need to send Email with that PDF file attachment.

How can i do that using renderAs

 

sguptasgupta
if possible post the code or use case
PremanathPremanath

<apex:pageBlockTable value="{!products}" var="p">
<apex:column >
<apex:inputCheckbox value="{!p.selected}"/>
</apex:column>
<apex:column value="{!p.oli.PricebookEntry.Product2.Name}"/>
<apex:column value="{!p.oli.Quantity}"/>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:commandButton value="Selected" action="{!Selected}"/>

</apex:pageBlockButtons>



public PageReference Selected(){
PageReference pdfPage ;
try{
outblock=true;
selectedprod= new List<OpportunityLineItem>();
for(cProduct eprod: getproducts()) {
if(eprod.selected == true){
selectedprod.add(eprod.oli);
}
}
System.Debug('*******************'+selectedprod);
pdfPage = page.product_delivery;//new PageReference('/apex/product_delivery1');
pdfPage.getParameters().put('id',oppid);
pdfPage.setRedirect(false);
Blob b=pdfPage.getContentAsPDF();

Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.setContentType('application/pdf');
attach.setFileName('deliverynote.pdf');
attach.setBody(b);

String[] toAddresses = new String[] {oppAcc.Account.Email__c};

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(toAddresses);
mail.setSubject('Delivery note');
mail.setHtmlBody('Here is the email you requested! Check the attachment!');
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });

// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
Catch(Exception e){
}
return null;
}

sguptasgupta

see if this wud help

 

<apex:pageBlockTable value="{!products}" var="p" rendered="{!hide}">
<apex:column >
<apex:inputCheckbox value="{!p.selected}"/>
</apex:column>
<apex:column value="{!p.oli.PricebookEntry.Product2.Name}"/>
<apex:column value="{!p.oli.Quantity}"/>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:commandButton value="Selected" action="{!Selected}" rerender="block2" oncomplete="sendmail"/>

 

<apex:actionfunction name="sendemail" action="{!SElectedSendemail}" />

 

<apex:pageBlockTable value="{!selectedprod}" var="p" rendered="{!show}" id="block2">
<apex:column >
<apex:inputCheckbox value="{!p.selected}"/>
</apex:column>
<apex:column value="{!p.oli.PricebookEntry.Product2.Name}"/>
<apex:column value="{!p.oli.Quantity}"/>
</apex:pageBlockTable>

</apex:pageBlockButtons>

This was selected as the best answer
PremanathPremanath

Hi Gupta,

 

 

   This is only the Better solution, i did some changes and finally i reached.

   Thanks a lot,

 

Prem

hamshuhamshu
HI premanath,

Can you share the code .i am trying same thing but can not get it.

Thanks
PremanathPremanath

<apex:pageBlockTable value="{!products}" var="p" rendered="{!hide}">
<apex:column >
<apex:inputCheckbox value="{!p.selected}"/>
</apex:column>
<apex:column value="{!p.oli.PricebookEntry.Product2.Name}"/>
<apex:column value="{!p.oli.Quantity}"/>
</apex:pageBlockTable>

<apex:pageBlockTable value="{!selectedprod}" var="p" rendered="{!show}">
<apex:column value="{!p.PricebookEntry.Product2.Name}"/>
<apex:column value="{!p.Quantity}"/>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:commandButton value="Selected" action="{!Selected}" oncomplete="sendmail();" reRender="pb"/>
</apex:pageBlockButtons>
<apex:actionfunction name="sendmail" action="{!SendAttach}" />

 

 

 

 

 

public PageReference Selected(){
show=true;
hide=false;

try{
selectedprod= new List<OpportunityLineItem>();
for(cProduct eprod: getproducts()) {
if(eprod.selected == true){
selectedprod.add(eprod.oli);
}
}
//System.Debug('*******************'+selectedprod);

//SendEmail();

}
Catch(Exception e){
}
return null;
}
public PageReference SendAttach(){
PageReference pdfPage ;
pdfPage = page.product_delivery;//new PageReference('/apex/product_delivery1');
pdfPage.getParameters().put('id',oppid);
pdfPage.setRedirect(false);
Blob b=pdfPage.getContentAsPDF();
// attach the pdf to the Opportunity
Attachment attach = new Attachment();
attach.Body = b;
attach.Name = 'deliverynote.pdf';
attach.ParentId = oppid;
insert attach;

}

arronleearronlee

I have only tried to create PDF files with the help of some PDF tools. You can also google it and select a PDF SDK whose way of processing is simple and fast to help you with the related work. Remember to check its free trial package first if possible. I hope you success. Good luck.



Best regards,
Arron