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
ThomasmThomasm 

visualforce page to display content

i am trying to use a visualforce page to display the contents of a document, but when the page loads i get the following error

common.apex.runtime.impl.ExecutionException: BLOB is not a valid UTF-8 string

<apex:page renderAs="pdf" controller="basicWeightPDF" contentType="HTML" >
<html>
<body>
{!dipslaytable}
</body>
</html>
</apex:page>

public class basicWeightPDF{
public string dipslaytable{get;set;}
public basicWeightpdf()
{
document d=[Select id, body from document where id='015190000000W33'];
Blob bodyOfAttachment = EncodingUtil.base64Decode('JVBERi0xLjIgCiXi4');
string strOrgId=UserInfo.getOrganizationID();
dipslaytable=d.body.tostring();
}
}

ShashForceShashForce
Hi,

Instead of:

dipslaytable=d.body.tostring();

Please try:

dipslaytable = EncodingUtil.base64Encode(d.body);

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
EnreecoEnreeco
I think the problem is that your BLOB has not a "text" value but a bynary value.
You can use the EncodingUtil.base64Encode but it diplays only the base 64 of the content.
If what you want to do is to show an image, you should use something like
<img src="data:image/png;base64,{!displayTableBase64}"/>

ThomasmThomasm
i changed it like you said but now it gets an error on the page.  Acroba may not display the page correclty.  Please contact the person who create.   when i open the file normally its fine
EnreecoEnreeco
Try to remove the renderAs="pdf" attribute in the page and see if that works (it will be a simple HTML page, but it the "img" tag is correct you should see the image in the page).
Or maybe you have to change the "data:image/png" to "data:image/jpeg" or data:image/bmp or whatever your image is...it may be in the COntentType field of your attachment (but it depends on how the attachment has been added)
ThomasmThomasm
when i remove render as the pdf goes to download and not open on the page and when i so the img src is only shows where i picture should be
EnreecoEnreeco
Try to remove also the "contentType" on the page and see what happens
ThomasmThomasm
removing content type change anything.  it doesnt seem to want to display it as an image
EnreecoEnreeco
Can you post here your page? Which kind of image is stored inside the Document?
ThomasmThomasm

here is the current page

<apex:page controller="basicWeightPDF"  >
<html>
<body>
<img src="data:image/jpg;base64,{!dipslayTable"/>

</body>
</html>
</apex:page>

and controller

public class basicWeightPDF{
public string dipslaytable{get;set;}
public basicWeightpdf()
{
document d=[Select id, body from document where id='015190000000W33'];
Blob bodyOfAttachment = EncodingUtil.base64Decode('JVBERi0xLjIgCiXi4');
string strOrgId=UserInfo.getOrganizationID();
dipslaytable = EncodingUtil.base64Encode(d.body);
//d = EncodingUtil.urlEncode(d, 'ASCII');
//d = EncodingUtil.urlDecode(d, 'ASCII');
}
}

 

the document is just a word doc with a jpg

EnreecoEnreeco
I thought that the Document object was an image. You cannot display a WORD file inside an html file doing this (even if you set the "pdf" renderAS).
If all you want to do is make the user download the Document, simply make a redirect to the document itself:
<apex:page controller="basicWeightPDF"  action="{!donwloadDocument}">
</apex:page>
public class basicWeightPDF{
     public PageReference downloadDocument(){
       Document doc = ... query document ...;
       PageReference pg = new PageReference('/servlet/servlet.FileDownload?file='+doc.Id);
      pg.setRedirect(true);
      return pg;
     }
}
try this!
Hope this helps