Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making embedded PDF scrollable in iPad

Tags:

html

pdf

ipad

I have a PDF embedded in HTML using the object tag. The embedded PDF is a big document and when viewed from my desktop the PDF is displayed properly with scrollbar in all the browsers including safari. However when I view the same html page in iPad the embedded PDF does not have a scrollbar. Is there any way in which we can show the scrollbar in iPad for an embedded PDF document.

The code used for embeding the PDF is

<object data="pdf.pdf" type="application/pdf" width="1000px" height="1200px" id="pdfDoc" name="pdfDoc"></object>

I tried with iframe too and even that does not work.

like image 486
Albin Joseph Avatar asked Nov 23 '11 05:11

Albin Joseph


2 Answers

This seems to work:

  • make the object tag big enough to show the whole PDF, and
  • contain it in a div with limited height and overflow:auto -- add -webkit-overflow-scrolling in iOS 5+ for good, native scrolling.

Here's the code I used:

<!DOCTYPE html>
<html>
  <head>
    <title>PDF frame scrolling test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <style>
      #container { overflow: auto; -webkit-overflow-scrolling: touch; height: 500px; }
      object { width: 500px; height: 10000px }
    </style>
  </head>
  <body>
    <div id="container">
      <object id="obj" data="my.pdf" >object can't be rendered</object>
    </div>
  </body>
</html>
like image 148
Dave Burt Avatar answered Sep 20 '22 21:09

Dave Burt


I needed the same thing, so here I share.

Issues I faced:

  • Cropped content : iframe on iOS (iPad) content cropping issue
  • Scrollbars not shown : Making embedded PDF scrollable in iPad
  • Cannot make full width

Please try the following:

http://jsfiddle.net/aknMJ/2/embedded/result/

Used tricks:

  1. Read the document width and scale the PDF frame according to that
  2. "width:100%" does not work with iframes in iPad, so I needed to use CSS3 transformations
  3. Wait until the PDF is completely loaded and then show&resize the PDF frame. Otherwise the content was cropped.
$('#pdfFrame').hide();
var pdfFrame = document.getElementById('pdfFrame');
pdfFrame.contentWindow.location.replace(PDF_URL);
$('#pdfFrame').on('load', function () {
    $('#pdfFrame').show();
    var documentWidth = $(document).width()
    var scale = (documentWidth / width) * 0.95;
    $('#pdfFrame').css("-webkit-transform", "scale(" + scale + ")");
});
like image 28
Ali Ok Avatar answered Sep 21 '22 21:09

Ali Ok