+ Start a Discussion
RajaMohanRajaMohan 

How to decode the PDF?

Hi Folks,

 

I am getting a pdf/word/excel file from the System and storing the binary information in a separate object. When I try to decode it in the attachment it is decoding in the right format but the file is not opening. It is giving Failed to open the pdf documnet error.

 

//In a separate method

fileBody=EncodingUtil.base64Encode(myfile.body);

 

DateTime todayDate=datetime.now();
ts=todayDate.format('yyyyMMddHHmmss');

Decimal ord=0.0;
List<Attachment_Creation__c> attList=new List<Attachment_Creation__c>();

while(fileBody!=null){
String str;
Attachment_Creation__c attcre=new Attachment_Creation__c();
if(fileBody.length()>32000){
str=fileBody.substring(0,32000);
filebody=fileBody.substring(32001);
attcre.File_Body__c=str;
attcre.Timestamp__c=ts;
attcre.Order__c=ord++;
attList.add(attcre);
}
else {
str=fileBody.substring(0);
attcre.File_Body__c=str;
attcre.Timestamp__c=ts;
attcre.Order__c=ord++;
attList.add(attcre);
fileBody=null;
}
}//while ends

if(attList.size()>0)
insert attList;

 

 

And, In the save method,

 

transient List<Attachment_Creation__c> att=[select Timestamp__c,File_Body__c,Order__c from Attachment_Creation__c where Timestamp__c=:ts order by Order__c asc];
String b='';
if(renderAttachmentSec && att.size()>0){
Attachment a = new Attachment(parentId = createAction.id, name=myfile.name);
for(Attachment_Creation__c sa:att){
b=b+sa.File_Body__c;
}
System.debug('£££ Body Save : '+b);

if(a.Name.contains('pdf')){
a.Name=a.Name+'.pdf';
a.ContentType='application/pdf';
// Blob bb=Blob.valueOf(b.trim());
// String s=EncodingUtil.base64Encode(bb);
a.Body=EncodingUtil.base64Decode(b.trim());
}

 

The reason I am doing in this way is, to avoid the View state issue. Please explain the right way to decode it and open the pdf.

 

Thanks

Raja

Best Answer chosen by Admin (Salesforce Developers) 
RajaMohanRajaMohan

Yes I can open it without any errors.

 

I had done a small work around to complete the task. I created a new document in the same name of the Attachment file and queried the same file by using the name in another (Save) method. At last, I deleted the document in the save method.

 

This worked for me in uploading the huge size of file from the visualforce page. 

 

Thanks for your interest. 

 

 

Raja

All Answers

Jia HuJia Hu
Don't do the Encode/Decode if you only want to transfer the Content in the Salesforce among different objects.
The format of the Content is all the same as base64 among the attachment, document, contentversion objects.

For a simple case, if you want to move a pdf from Document to Attachment, just like,
Document doc = [Select Id, Name, Body from Document order by createddate desc limit 1];
Attachment att = new Attachment();
att.Body = doc.Body;
att.Name = 'att.pdf';
att.ParentId = '001N0000003BOTu';
insert att;
RajaMohanRajaMohan

Hi Jia,

 

Thanks for your reply. But this solution will not work for me because, I have to attach a big file in a visualforce page. This is adding a new attachment. By using the visualforce page I can able to attach only a minimum capacity of the file (I think it is upto 100kb.) The file which I am trying to upload is a bigger one in size. Kindly note that this is an insert operation to the attachment.

 

Do you have any work around for this?

 

 

Thanks

Raja

Jia HuJia Hu
Yeah the limitation of Attachment is 5MB.
As you mentioned, when you uploaded a small file, and then you download it, can you open it with no errors?
RajaMohanRajaMohan

Yes I can open it without any errors.

 

I had done a small work around to complete the task. I created a new document in the same name of the Attachment file and queried the same file by using the name in another (Save) method. At last, I deleted the document in the save method.

 

This worked for me in uploading the huge size of file from the visualforce page. 

 

Thanks for your interest. 

 

 

Raja

This was selected as the best answer
arronleearronlee

I can open PDF files successfully, too. Have you installed some 3rd party PDF processing tools to help you with your work? I am not a common user about document and image manipulating. As for myself, I have only tried the free trial package of an image and document conversion tool to decode PDFs and do each step according to its tutorials. It is just one of many but I do appreciate it because it is totally manual. Even though I only tried its free trial package to generate and read PDFs and didn′t check the cost and licensing conditions, it worked great for me. Besides, this site also offers many other choices for processing different kinds of document and image formats. Share with you. And I hope you success. Good luck.



Best regards,
Arron