13

We would like to show the PDF document in an HTML page after retrieving it from the database via a servlet's doGet method.

Can anyone share some snippets on how to achieve this?

John
  • 10,154
  • 9
  • 79
  • 143
user339108
  • 11,603
  • 31
  • 78
  • 111

3 Answers3

22

Non-html content apart from images needs to be retrieved using an object, embed or iframe tag

  1. iframe: <iframe src="somepage.pdf"></iframe>
  2. object/embed: <object src="somepage.pdf"><embed src="somepage.pdf"></embed></object>

somepage.pdf could be somepage.jsp?filename=somepage&mimetype=application/pdf

Here is an interesting link How to Embed Microsoft Office or PDF Documents in Web Pages

and here is a stackoverflow search

Community
  • 1
  • 1
mplungjan
  • 134,906
  • 25
  • 152
  • 209
  • 1
    I need to use inline style elements from the parent document in the child document. Iframe doesn’t allow this. Would this work with embed or object? – user2284570 Aug 25 '15 at 23:31
  • Unlikely. If cross domain it will also not work. If not, then iframe should work. Ask a separate question – mplungjan Aug 26 '15 at 03:44
  • same domain. But the question is already asked and it's answers only provide response for using external stylesheets. – user2284570 Aug 26 '15 at 08:53
  • So why can't you use iFrame? You can copy and insert the parent's stylesheets into the iframe when loading – mplungjan Aug 26 '15 at 09:02
  • In fact I don't need an iframe. This is for a single tag which is pre. The content require a special editor that I wrote myself and it's very difficult to edit it. So I thought the best was to split that part for the main document. Also the solution should not require visitors to enable JavaScript. – user2284570 Aug 26 '15 at 09:12
  • 2
    When i'm trying these, it's start downloading the docx file, is it because i'm not on a server (offline), or im' missing something? – Mousa Alfhaily Apr 06 '17 at 08:45
  • Likely your browser is not set up to use word embedded. – mplungjan Apr 06 '17 at 08:50
  • This will work only for PDF, for any Microsoft Office format it will download the file. I even tested it on Edge, and still I didn't managed to View a .docx file. Did anyone managed to make this work?? – Gil Epshtain Dec 24 '19 at 16:55
10

Google docs viewer can handle this.

try

<html>
    <head>
        <style>
            *{margin:0;padding:0}
            html, body {height:100%;width:100%;overflow:hidden}
        </style>
        <meta charset="utf-8">
            <?php
                 $url = $_GET['url'];
             ?>
        <title><?php echo $url; ?></title>
    </head>
    <body>
        <iframe src="http://docs.google.com/viewer?url=<?=urlencode($url)?>&embedded=true"  style="position: absolute;width:100%; height: 100%;border: none;"></iframe>
    </body>
</html>
Breeze
  • 1,824
  • 2
  • 30
  • 37
lonestar
  • 347
  • 1
  • 5
  • 13
0

If you are looking for a pure HTML version of the document (for fast rendering and cross browser support), you can go through Docspad. Docspad helps embedding all the different popular document formats into your web application. http://docspad.com

Ajay Narang
  • 308
  • 2
  • 9