PDF in webView?

You must Login before you can answer or comment on any questions.

I'm trying to get a PDF to load in a WebView and it just isn't happening. I keep getting the error "[ERROR] Couldn't determine the proper encoding. Make sure this file: 5_preludes.pdf is UTF-8 encoded." The KitchenSink app displays the PDFs just fine (even mine if I sub out the file, location, and string). Any ideas? Put another way, what kind of critter do I have to sacrifice to get this to work? there are possums, moles, and raccoons nearby (though I'd rather avoid them).

My code:

var scoreView = Titanium.UI.createWebView({
    url:"scores/5_preludes.pdf",
    top:0,
    height:"auto",
    width:"auto"
});
 
mainWindow.add(scoreView);

4 Answers

So, it ended up the following error was a red herring:

[ERROR] Couldn't determine the proper encoding. Make sure this file: 5_preludes.pdf is UTF-8 encoded.

The real culprit were the auto values for the webview (or its parent view). The following code worked for me on 3.2 and 4.0 SDK, TiSDK 1.4:

var win = Titanium.UI.createWindow({
    backgroundColor:"#fff"
});
var v = Titanium.UI.createView({
    top:0,
    left:0,
    width:1024, //Don't use "auto"
    height:450  //Don't use "auto"
});
 
var pdfViewer = Ti.UI.createWebView({
    url: "etd_2009.pdf", //Local PDF; also works with remote PDF
    width:1024, //Don't use "auto"
    height:250  //Don't use "auto"
});
 
v.add(pdfViewer);
win.add(v);
win.open();
The previously mentioned errors still throws but the app runs and the PDF is viewable.

— answered 3 years ago by Rafael Hernandez
answer permalink
1 Comment
  • I'm using lots of PDFs in my app and for each this utf-8 error is logged. Does Apple accept this? Has anyone experiences with this?

    I have no idea how to solve this problem... any news about it??

    — commented 10 months ago by Balz Rittmeyer

i can't view the pdf using webview...

var wv_Pdf=Ti.UI.createWebView({ data:'report.pdf', top:'0sp', touchEnabled:false, enableZoomControls:false, scalesPageToFit:true, loading:true
});

I solved this problem by using the data attribute instead of url. Keep in mind you MUST pass a BLOB or FILE and NOT a URL. Alloy example:

var pdf = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, '/data/document.pdf');
$.webview.setData(pdf);

To load pdf file, try to use data, not url. So your code should be look like this:

var pdfViewer = Ti.UI.createWebView({
    data: "etd_2009.pdf", //Local PDF; also works with remote PDF
    width:1024, //Don't use "auto"
    height:250  //Don't use "auto"
});

Your Answer

Think you can help? Login to answer this question!