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
ssfdcssfdc 

Generating PDF corruption

I'm stumped over this one, I have successfully gnerated a PDF and attached it to a record as an attachment before, this application is slightly different but whatever I try the pdf seems corupted when i try and view and wont load.

 

Some code bellow:

 

public class theController {


   public PageReference savePdf() {
   //Save pdf
   String theURL = 'mySalesforceInstance/apex/pdfPage';
   PageReference pdf = new PageReference(theURL);
   Attachment attach = new Attachment();

   Blob body;

   try {

   / / returns the output of the page as a PDF
   body = pdf.getContent();

   // need to pass unit test -- current bug
   } catch (VisualforceException e) {
   body = Blob.valueOf('Some Text');
}

attach.Body = body;
// add the user entered name
attach.Name = 'pdfName.pdf';
attach.IsPrivate = false;
// attach the pdf to the account
//attach.ParentId = loggedInUser.contactId;

attach.ParentId = '00130000013epqx';

insert attach;

return null;
}

}

 

I am rendering "theUrl" page as a pdf as I did before, but when the pdf is Attached to my record i cannot open it,

I saved this as .html file to view the contents and I get the following:

 

<script>
if (window.location.replace){
window.location.replace('https://login.salesforce.com/?ec=302&startURL=%2Fapex%2FpdfPage');
} else {;
window.location.href ='https://login.salesforce.com/?ec=302&startURL=%2Fapex%2FpdfPage';
}
</script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">

 

 


</head>


</html>

 

<!--
...................................................................................................
...................................................................................................
...................................................................................................
...................................................................................................
-->

 

 

Can anyone confirm that I'm not going mad as I built a similar appication before, has something changed??

 

I have triple checked my code, "theUrl" is correct etc.

 

Thanks.

 

 

 

 

 

 

 

 

ssfdcssfdc
Came across this article regarding permissions, but I'm logged in as System Admin, am i missing some setting?

http://boards.developerforce.com/t5/Visualforce-Development/Generated-PDFs-are-corrupt-only-in-production/td-p/99209
Juan SpagnoliJuan Spagnoli

Maybe is something wrong with the URL, try this:

 

PageReference pdf = Page.YourPage;
pdf.getParameters().put('id',theIdIfNecessary);
                    
// Take the PDF content
Blob b = pdf.getContent();      

 Best Regards

ssfdcssfdc

Juan you pointed me in the right direction!!, this will not work if you give it an absolute url: what worked was suppling the page reference as 'apex/pageName' ,, this makes NO sense as i'm looking as a similar application i created which uses the entire link VERY strange, hours wasted.

 

Thank you!!

Juan SpagnoliJuan Spagnoli

Great news!

 

But you don't need to uses a String like '/apex/yourPage', you can use:

 

PageReference pdf = Page.YourPage;

 

 It's safer that way.

 

Best luck! 

 

PD: Don't forget to set the post as solved for others users. :)