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

get PDF from external URL

hi guys,

i have a requirement to grab a pdf page from an external link, and attach it into an object.

by that i mean if you open the external link in a browser, it will come up as a pdf file.


i know how to attach it, but as far as i know the PageReference .getContent() can only work for visualforce page.


anyone know how to do this?


thank you in advance.



have you tried to use document and create a document out of the link???? 


hi vanessen, thanks for the reply.


could you give me a simple example?




is the link to the pdf generated by you or should you pass it via a parameter to the page ?


you can try this : 


public Attachment file = new Attachment();

file.url = 'http://......./sample.pdf'; // when using url do not specify the body and bodylength


and then you can attach the file to the object you want


suppose it is to be attached to case, 


file.ParentId  = newCase.Id; // new Case is a case object


insert file;


hope this help.



hi vanessen,

the url will be passed via parameter.

im clear with the attachment object which i usually used to attach a file.


but how to grab the pdf from the external url?

url to a pdf example:

i want to grab that file and attach it into (for example) case object.


i tried and checked the attachment object, it does not have the 'attachment.url' method.


thanks again.




i didn't try , but check out this link:|StartTopic=Content%2Fsforce_api_objects_document.htm|SkinName=webhelp
it does specify that there is URL method that is applicable for(create,filter,nillable and update).
and the description is as follow :
 URL reference to the file (instead of storing it in the database). If specified, do not specify the Body or BodyLength.



hi vanessen, thanks again for the reply.


i have tried using the Document.URL.

and as the description said, it will create a document that saves the url.

but it is not actually grabbing the page.



this is what i have so far:



global class PdfGrabber_WS {
	WebService static Integer PdfGrabber_WS(Id pdfGrabberId) {
		Integer state = 0;
		Pdf_Grabber__c pGrab = [SELECT Id, Name, Link__c FROM Pdf_Grabber__c WHERE Id = :pdfGrabberId];

PageReference pdf = new PageReference(pGrab.Link__c); Blob pdfBody; try { pdfBody = pdf.getContent(); } catch (Exception e) { pdfBody = Blob.valueOf('Unable to grab pdf content.'); } Attachment myAttach = new Attachment(); myAttach.parentId = pGrab.Id; = 'PDFFILE_' + pGrab.Name + '.pdf'; myAttach.body = pdfBody; insert myAttach; return state; } }


the code will run and attach a file, but when i tried to open the file, it says:

File does not begin with '%PDF-'