Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a PDF which is retrieved via web services calls. The PDF is sent over in a base64 encoded string. I would like to be able to visit a page and output the result of this request. Currently I am converting the encoded base64 string in to a binary blob (It doesn't seem apex supports byte arrays? strange..). Is there a way for me to output this to the screen for the user? I would like to not have to save this; just output it.

Currently, I am using this:

Controller:

Blob PDFOutputBOL =  EncodingUtil.base64Decode(match1);

Page:

<apex:page controller="DownloadPDF" sidebar="false" showHeader="false" contentType="application/pdf">
{!PDFOutputBOL}
</apex:page>

Checking my HTTP response, I get the following:

HTTP/1.1 200 OK Date: Tue, 15 Jan 2013 14:18:36 GMT X-Powered-By: Salesforce.com ApexPages P3P: CP="CUR OTR STA" Cache-Control: no-cache, must-revalidate, max-age=0, no-store, private Pragma: no-cache Expires: Tue, 15 Jan 2013 14:18:36 GMT Content-Type: application/pdf; charset=UTF-8 Content-Length: 40

core.filemanager.ByteBlobValue@6ed6c5ba

What can I do to just print the PDF?

share|improve this question

1 Answer 1

up vote 8 down vote accepted

You can embed a PDF in a Visualforce (or any other HTML) page via a data URI. Using your example, the code would be something like:

<apex:page controller="DownloadPDF" sidebar="false" showHeader="false" contentType="application/pdf">
    <script>
        window.location.href = "data:application/pdf;base64,{!match1}";
    </script>
</apex:page>

Note that you need the base64 encoded data - in your example, match1, rather than PDFOutputBOL.

Caveat: I don't know how widely this is supported. Results of some informal testing:

Works on:

  • Mac OS X 10.6.8
    • Chrome 23.0.1271.101
    • Safari 5.1.7
  • Windows 7 SP1
    • Chrome 24.0.1312.52
    • Safari 5.1.7

Doesn't work on:

  • Mac OS X 10.6.8
    • Firefox 17.0.1
  • Windows 7 SP1
    • IE 9.0.8112.16421
    • Firefox 18.0

You may be able to get it to work on those browsers with the appropriate plugin.

share|improve this answer
1  
Hi, I appreciate the help but I don't think this is going to work. Surely there has to be a way to get the byte stream from base64. This is one line of code in every single programming language. Why it seems to be disabled in Apex is beyond me. I was able to get one pdf to load by doing the blob.toString() method, but as soon as I loaded a more complex error, it began to complain it wasn't utf-8 encoded. Does anyone else have any experience with this? –  Kyle Jan 15 '13 at 18:44
    
Did you try it? This approach works for me consistently on Chrome and Safari. You are correct - the Blob class is lacking several methods that would make it much more useful. We are working on adding them in a future release. –  metadaddy Jan 15 '13 at 18:48
    
Thanks again for the response. I think we're going to have to settle with this as a solution. I was looking for something that would support more traditional browsers (IE; ie, firefox). Talking to the users though, they are using Chrome as is so this will be considered closed. If you're a developer for Salesforce (notice the 'we' thing there.), for the sake of my (and others like me) sanity, is there any intention to include byte-types, or more preferably, an array of them? They are listed as a type on the salesforce website but appear to be disabled. Anywho, thanks again. –  Kyle Jan 16 '13 at 15:29
    
I'm a developer evangelist at Salesforce, so this is part of what I do. Look for a better Blob class over the next few releases #safeharbor –  metadaddy Jan 17 '13 at 3:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.