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
dgindydgindy 

Upload in Visualforce Page

Is it possible to upload a PDF in a visual force page?
 
Is it possible to save a PDF created by a visual force page as an attachment to a contact?
 
 
KeithJKeithJ
You're in luck! It is possible with the use of the visualforce tag:

Code:
<apex:inputFile 

 
The real magic happens in your controller where you can reference your uploaded file with the use of
a
Code:
Blob

 Check out the Apex reference for that one. You can then instantiate an Attachment object and assign the blob to the Attachment body.
After that, do a DML insert, upsert or whatever is necessary.

dgindydgindy
Ok.  So how would I create a PDF in Visualforce and attach it to a document all in one movement?
 
 
VisualForceVisualForce

Hi..

  My idea is you have to use two vf page and custom button..

In first page u write a code for save PDF usnig blob..

In second page u call ur pdf page using renderas pdf 

In custom button u have to call this two vf pages...
dgindydgindy
Code:
public PageReference CommitFile() {
  
                
        Attachment attachment = new Attachment(); 
        PageReference Pg = new PageReference(URL);
        attachment.Body = Pg.getContent(); 
        attachment.Name = 'Test3.PDF';
        attachment.ParentId = '0014000000JUDbb';
        
        insert attachment;
        
        return null;
    }

 
my solution
KeithJKeithJ


dgindy wrote:
Code:
public PageReference CommitFile() {
  
                
        Attachment attachment = new Attachment(); 
        PageReference Pg = new PageReference(URL);
        attachment.Body = Pg.getContent(); 
        attachment.Name = 'Test3.PDF';
        attachment.ParentId = '0014000000JUDbb';
        
        insert attachment;
        
        return null;
    }

 
my solution


Nice, what does the Pg.getContent() return? Is it a reference to a blob?
Cheers
dgindydgindy
Yes.  It return a Blob.  you don't have to encode it either. 
KeithJKeithJ
Kudos! :)