Sunday, January 29, 2012

Upload .csv file and save it as a PDF file in Document Object

In this article I am going to tell you how to upload a ".csv" file into salesforce,convert it into PDF file and storing that converted PDF file into Document Object. In order to do this task, we need to create following VF pages along with their controllers :
  1. VF page that will take .csv file as input.
  2. VF page that will be rendered as PDF to show the csv file's data.
First of all let us create VF page and its controller that would be rendered as PDF. Lets name that page as "PDFPage.page".

   
      
         {!content}
      
   


PDFPageController :
public with sharing class PDFPageController {
   public String excelData{get;set;}
   public List< String > contentList{get;set;}
 
   public PDFPageController(){
     excelData='';
     excelData=Apexpages.currentPage().getParameters().get('data');
     if(excelData!=''){
        String[] sArray=excelData.split('\n');
        contentList=new List< String >();
 
        for(String str:sArray){
           contentList.add(str);
        }
     }
   }
}
  • Whenever we will reference this page, we will send the excel data as a parameter to page. 
  • So in the constructor, we have to retrieve data that is to be displayed.
  • We cannot directly use this excel data to print its content into the PDF file because if we do so the output in PDF will be like "a,b\nc,d\ne,f\n.....". It does not understand the line break character - "\n" and prints it as it is. So the solution is to split the string based on "\n" and form a List of String.
  • In VF page, the contents are shown using <apex:repeat> tag by iterating on string list.
We are done with the PDF part. Lets proceed by creating VF page for taking csv file as input and preparing its PDF blob.
 
   
     
        
   
 
ExcelDemoController :
 public with sharing class ExcelDemoController {
   public Document document{get;set;}
   public String excelData{get;set;}
   
   public ExcelDemoController()
   {
      document= new Document();
      excelData='';  
   }
 
   public void readExcelData()
   {
      if(document!=null)
      {
         //assign data contained in csv file to String variable
         excelData=document.body.toString();
  
         //PDF page which we created in the first step
         PageReference pdfPage =  Page.PDFPage;

         //set excel data as parameter to PDFPage.
         pdfPage.getParameters().put('data',excelData); 
         pdfPage.setRedirect(true);
   
         // Take the PDF content 
         Blob b = pdfPage.getContent();
 
         //Create Document Object
         document.Body=b;
         document.name='test.pdf';
         document.FolderId=UserInfo.getUserId();

         //save the pdf file
         upsert document;
       }
   }
}
  • The document object will be created with "test.pdf" name. You can change this name with the desired name.
Thats all !!!. Now you are ready to convert a csv file into PDF.

7 comments:

  1. Good job

    ReplyDelete
  2. Hi Dawal,

    I built a Site in salesforce from visualforce pages. The site refers to custom object in salesforce and displays data. It works fine, but now I am trying to show attachments in site. I am not sure how to implement this. Do I have to build a new controller or can I utilize the existing controller?

    ReplyDelete
  3. Hi Dhawal,

    Nice work!!!! I built a site in salesforce using visualforce pages and controller. The site refers to custom object in salesforce and displays details. It is working fine, but now I am trying to show attachments in the site and not sure how to implement this. Do I have to build a separate controller or can I utilize the existing? Thanks in advance.

    ReplyDelete
  4. Congratulations guys, quality information you have given!!!..Its really useful blog. Thanks for sharing this useful information..

    Salesforce Course in Chennai

    ReplyDelete