PDF in webView?
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);
6 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.
-
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 ofurl
. 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);
-
·Add a handler DocumentWebViewer.ashx to your project so the viewer can communicate with it.
·VS will add a default web handler. Please replace the whole contents in it with the following codes.
Tutorials on how to creat PDF Web Viewer:<scripttype="text/javascript"language="javascript"> var_docUrl='Images/Example.tif'; var_serverUrl='WebDocViewer.ashx'; var_viewer=newAtalasoft.Controls.WebDocumentViewer({ 'parent':$('#_container1'), //parentcontainertoputtheviewerin 'toolbarparent':$('#_toolbar1'), //parentcontainertoputtheviewertoolbarin 'serverurl':_serverUrl, //serverhandlerurltosendimagerequeststo 'documenturl':_docUrl //documenturlrelativetotheserverhandler url }); </script>
-
Reading pdf in a web viewer needs no footprint, no flash plugin, if you are familiar with asp.net and javascript control, you can create a pdf web viewer.
-
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" });