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
Scott0987Scott0987 

PDF Attachment won't open

I have a page that is a license agreement.  It is a simple visualforce page.  I have renderas="PDF" set on it and it renders correctly as a PDF document.  What I then want to do is using a second page attach the first page to an opportunity as an attachment.   It seems to work but the attachment will not open.  My PDF program just says Loading...  Here is my class:

public with sharing class testing2ext {

	public blob pdfDoc {get;set;}
	public Attachment att {get;set;}

	public void runAtStart(){
		PageReference pdfPage = new PageReference('/apex/testing?id='+apexpages.currentpage().getparameters().get('id'));
		pdfDoc = pdfPage.getContentAsPdf();
		att = new Attachment();
		att.body=pdfDoc;
		att.ContentType='PDF';
		att.IsPrivate=false;
		att.Name='testing.pdf';
		att.ParentId=apexpages.currentpage().getparameters().get('id');
		insert att;
	}

}

 Any ideas why the attachment will not open after being attached?

Best Answer chosen by Admin (Salesforce Developers) 
Scott0987Scott0987

For anyone that runs into this the issue I had was the line 

att.ContentType='PDF';

 The minute I took that out it started working.  Not sure why it broke it but that was my problem.


Scott0987 wrote:

I have a page that is a license agreement.  It is a simple visualforce page.  I have renderas="PDF" set on it and it renders correctly as a PDF document.  What I then want to do is using a second page attach the first page to an opportunity as an attachment.   It seems to work but the attachment will not open.  My PDF program just says Loading...  Here is my class:

public with sharing class testing2ext {

	public blob pdfDoc {get;set;}
	public Attachment att {get;set;}

	public void runAtStart(){
		PageReference pdfPage = new PageReference('/apex/testing?id='+apexpages.currentpage().getparameters().get('id'));
		pdfDoc = pdfPage.getContentAsPdf();
		att = new Attachment();
		att.body=pdfDoc;
		att.ContentType='PDF';
		att.IsPrivate=false;
		att.Name='testing.pdf';
		att.ParentId=apexpages.currentpage().getparameters().get('id');
		insert att;
	}

}

 Any ideas why the attachment will not open after being attached?