+ Start a Discussion
sonatinesonatine 

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.

 

vanessenvanessen

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

sonatinesonatine

hi vanessen, thanks for the reply.

 

could you give me a simple example?

 

 

vanessenvanessen

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.

 

sonatinesonatine

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: http://www.lc.unsw.edu.au/onlib/pdf/plag.pdf

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.

 

vanessenvanessen

 

i didn't try , but check out this link:
http://www.salesforce.com/us/developer/docs/api/index_Left.htm#CSHID=sforce_api_objects_document.htm|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.

 

sonatinesonatine

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.

 

sonatinesonatine

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; myAttach.name = '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-'