2011-05-31 68 views
8

我將pdf文檔嵌入到我的html代碼中。爲此我寫了這段代碼。使用對象標籤在html中嵌入PDF文件

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org 

/TR/html4/loose.dtd"> 
<html lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
    <title></title> 
</head> 
<body> 
<object height="100%" width="100%" type="application/pdf" data="yii.pdf#toolbar=1&amp;navpanes=0&amp;scrollbar=1&amp;page=1&amp;view=FitH"> 

<p>It appears you don't have a PDF plugin for this browser. No biggie... you can <a href="/pdf/sample.pdf">click here to download the PDF file.</a></p> 

</object> 

</body> 
</html> 

但結果是FF4上的空白頁面和IE9嵌入PDF文件,但它的容器幾乎是頁面的30%。如果我刪除第一行,即DOCTYPE,那麼這兩個瀏覽器都會呈現pdf文件。 以下代碼正常工作。

<html lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
    <title></title> 
</head> 
<body> 
<object height="100%" width="100%" type="application/pdf" data="yii.pdf#toolbar=1&amp;navpanes=0&amp;scrollbar=1&amp;page=1&amp;view=FitH"> 

<p>It appears you don't have a PDF plugin for this browser. No biggie... you can <a href="/pdf/sample.pdf">click here to download the PDF file.</a></p> 

</object> 

</body> 
</html> 

我想在我的頁面中使用doctype,以便其他頁面正常工作。有沒有辦法解決包含doctype的第一個代碼?

回答

5

我會嘗試使用元素。
如果沒有,也許將其轉換爲閃光燈,然後嵌入閃光燈。

同時,儘量<!doctype html>,看看它做什麼,這對HTML5

+1

Upvoted 6年後,因爲'iframe'似乎仍然是要走的路。 – 2017-11-12 14:29:03

9

這基本上是@ TXK的答案(+1)標準的文檔類型,但有工作(戰鬥測試)代碼:

<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>Preview</title> 
<style type="text/css"> 
html, body { 
    margin: 0; 
    padding: 0; 
    border: 0; 

    height: 100%; 
    overflow: hidden; 
} 
iframe { 
    width: 100%; 
    height: 100%; 
    border: 0 
} 
</style> 
</head> 

<body> 

<iframe src=""></iframe> 

</body> 
</html> 
+0

謝謝!完美地工作,正是我所需要的。 – 2013-09-27 14:35:00

6

有三種方法可以在HTML中顯示PDF:使用嵌入,對象或iframe。不幸的是,使用iframe將不允許PDF中的Adobe Javascript 將消息發佈到HTML中的JS,因爲hostContainer未定義。所以我被迫使用嵌入或對象。幸運的是thirtydot的樣式代碼也適用於對象。這裏是我的工作代碼...

<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>Preview</title> 
<style type="text/css"> 
html, body { 
    margin: 0; 
    padding: 0; 
    border: 0; 
    height: 100%; 
    overflow: hidden; 
} 
object { 
    width: 100%; 
    height: 100%; 
    border: 0 
} 
</style> 
<script language="javascript"> 
    function handleMessage(msg) { 
     alert('got message '+msg); 
    } 
    function setupHandler() { 
     document.getElementById("myPdf").messageHandler = { onMessage: handleMessage }; 
    } 
</script> 
</head> 
<body onLoad="setupHandler();"> 
<object id="myPdf" type="application/pdf" data="file_with_actions.pdf"> 
Unable to open the PDF file. 
</object> 
</body> 
</html> 

你可以閱讀更多關於Javascript的東西here

+1

另一個發現:如果您使用SWT瀏覽器顯示PDF,請勿使用setText,否則hostContainer未定義。您必須使用setURL(即將文本放在臨時文件中)。 – 2013-10-01 21:13:36

相關問題