Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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);
— asked August 18th 2010 by Rafael Hernandez
  • ipad
  • pdf
0 Comments

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.

    — answered August 29th 2010 by Rafael Hernandez
    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 August 23rd 2012 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
    });

    — answered September 18th 2012 by Gowshick D
    permalink
    1 Comment
    • Define height and width and that work.

      — commented August 7th 2013 by Nabeel Munawar
  • 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);
    
    — answered January 10th 2013 by Daniel Mahon
    permalink
    0 Comments
  • ·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>
    
    — answered September 2nd 2013 by arron lee
    permalink
    0 Comments
  • 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.

    — answered October 21st 2013 by HILLARY HALL
    permalink
    1 Comment
    • Yup indeed, you can also creating a PDF web viewer using some manual tools. I am also testing about it. We can talk about it later. Good luck.

      Best regards,
      Arron

      — commented January 28th 2014 by arron lee
  • 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"
    });
    
    — answered May 3rd 2011 by Kusmayadi Z.A.
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.