编写 HTML 代码在浏览器中打开 PDF
Write HTML code to open PDF in browser
我从 word 文档生成一个 html 文件,其中有一些指向 pdf 文件的链接。当我在某些浏览器中打开它时,它们会出现下载提示。我需要的是在同一浏览器中打开 pdf 文件 window。
我读到一种解决方案是在 header 文件中包含以下命令:
Content-Type: application/pdf
Content-Disposition: inline; "filename.pdf"
但是,我不知道我必须将它们放在 pdf 文件的哪一部分。这是我的 html 代码的 header。
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=Generator content="Microsoft Word 14 (filtered)">
<base href=x>
<title>Welcome to SELSE-1, which we anticipate will be the first of a series of
annual workshops to bring together experts from both industry and academia to
discuss current and future trends in Soft Error Upset (SEU) rates and the state
of the art and possible</title>
<style>
<!--
/* Font Definitions */
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:"MS Mincho";
panose-1:2 2 6 9 4 2 5 8 3 4;}
@font-face
{font-family:"MS Mincho";
panose-1:2 2 6 9 4 2 5 8 3 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
{font-family:"Lucida Grande";}
@font-face
{font-family:"\@MS Mincho";
panose-1:2 2 6 9 4 2 5 8 3 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
h2
{mso-style-link:"Heading 2 Char";
margin-right:0in;
margin-left:0in;
font-size:18.0pt;
font-family:"Times New Roman","serif";
font-weight:bold;}
p.MsoCommentText, li.MsoCommentText, div.MsoCommentText
{mso-style-link:"Comment Text Char";
margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
p.MsoHeader, li.MsoHeader, div.MsoHeader
{mso-style-link:"Header Char";
margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
p.MsoFooter, li.MsoFooter, div.MsoFooter
{mso-style-link:"Footer Char";
margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;}
p
{margin-right:0in;
margin-left:0in;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
p.MsoCommentSubject, li.MsoCommentSubject, div.MsoCommentSubject
{mso-style-link:"Comment Subject Char";
margin:0in;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:"Times New Roman","serif";
font-weight:bold;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
{margin:0in;
margin-bottom:.0001pt;
font-size:8.0pt;
font-family:"Tahoma","sans-serif";}
p.MsoRMPane, li.MsoRMPane, div.MsoRMPane
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
span.maroon
{mso-style-name:maroon;}
span.HeaderChar
{mso-style-name:"Header Char";
mso-style-link:Header;
font-family:"Times New Roman","serif";}
span.FooterChar
{mso-style-name:"Footer Char";
mso-style-link:Footer;
font-family:"Times New Roman","serif";}
span.Title1
{mso-style-name:Title1;}
span.authors
{mso-style-name:authors;}
span.CommentTextChar
{mso-style-name:"Comment Text Char";
mso-style-link:"Comment Text";}
span.CommentSubjectChar
{mso-style-name:"Comment Subject Char";
mso-style-link:"Comment Subject";
font-weight:bold;}
span.Heading2Char
{mso-style-name:"Heading 2 Char";
mso-style-link:"Heading 2";
font-family:"Times New Roman","serif";
font-weight:bold;}
p.p1, li.p1, div.p1
{mso-style-name:p1;
margin-right:0in;
margin-left:0in;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
p.Default, li.Default, div.Default
{mso-style-name:Default;
margin:0in;
margin-bottom:.0001pt;
text-autospace:none;
font-size:12.0pt;
font-family:"Arial","sans-serif";
color:black;}
span.apple-converted-space
{mso-style-name:apple-converted-space;}
.MsoChpDefault
{font-size:10.0pt;}
/* Page Definitions */
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 89.85pt 64.9pt 89.85pt;}
div.WordSection1
{page:WordSection1;}
/* List Definitions */
ol
{margin-bottom:0in;}
ul
{margin-bottom:0in;}
-->
</style>
</head>
您不能强制浏览器显示 PDF,但您可以告诉它可以,如果它已经设置为显示的话。
您需要做的是使用您提到的 headers 提供文件。这告诉浏览器它正在加载的内容实际上是一个 PDF,如果它设置为打开 PDF,它就会打开。否则,可能会启动下载。
更具体地说,您需要将 headers 附加到 web-server 获取文件时给出的响应。实际应用程序可能会有很大差异,具体取决于您的 web-server 以及您提供文件的方式。
例如,如果文件是通过servlet提供的,你可以在servlet中设置content-type(java):
response.setContentType("application/pdf");
如果它是直接提供的(例如,您只是使用 链接到文件),那么您需要在服务器规模上进行设置。例如,在 tomcat 中,您在 web.xml 文件中这样设置:
<mime-mapping>
<extension>pdf</extension>
<mime-type>application/pdf</mime-type>
</mime-mapping>
这将服务器设置为任何带有 .pdf 扩展名的文件自动接收正确的 content-type。
我从 word 文档生成一个 html 文件,其中有一些指向 pdf 文件的链接。当我在某些浏览器中打开它时,它们会出现下载提示。我需要的是在同一浏览器中打开 pdf 文件 window。
我读到一种解决方案是在 header 文件中包含以下命令:
Content-Type: application/pdf
Content-Disposition: inline; "filename.pdf"
但是,我不知道我必须将它们放在 pdf 文件的哪一部分。这是我的 html 代码的 header。
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=Generator content="Microsoft Word 14 (filtered)">
<base href=x>
<title>Welcome to SELSE-1, which we anticipate will be the first of a series of
annual workshops to bring together experts from both industry and academia to
discuss current and future trends in Soft Error Upset (SEU) rates and the state
of the art and possible</title>
<style>
<!--
/* Font Definitions */
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:"MS Mincho";
panose-1:2 2 6 9 4 2 5 8 3 4;}
@font-face
{font-family:"MS Mincho";
panose-1:2 2 6 9 4 2 5 8 3 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
{font-family:"Lucida Grande";}
@font-face
{font-family:"\@MS Mincho";
panose-1:2 2 6 9 4 2 5 8 3 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
h2
{mso-style-link:"Heading 2 Char";
margin-right:0in;
margin-left:0in;
font-size:18.0pt;
font-family:"Times New Roman","serif";
font-weight:bold;}
p.MsoCommentText, li.MsoCommentText, div.MsoCommentText
{mso-style-link:"Comment Text Char";
margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
p.MsoHeader, li.MsoHeader, div.MsoHeader
{mso-style-link:"Header Char";
margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
p.MsoFooter, li.MsoFooter, div.MsoFooter
{mso-style-link:"Footer Char";
margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;}
p
{margin-right:0in;
margin-left:0in;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
p.MsoCommentSubject, li.MsoCommentSubject, div.MsoCommentSubject
{mso-style-link:"Comment Subject Char";
margin:0in;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:"Times New Roman","serif";
font-weight:bold;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
{margin:0in;
margin-bottom:.0001pt;
font-size:8.0pt;
font-family:"Tahoma","sans-serif";}
p.MsoRMPane, li.MsoRMPane, div.MsoRMPane
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
span.maroon
{mso-style-name:maroon;}
span.HeaderChar
{mso-style-name:"Header Char";
mso-style-link:Header;
font-family:"Times New Roman","serif";}
span.FooterChar
{mso-style-name:"Footer Char";
mso-style-link:Footer;
font-family:"Times New Roman","serif";}
span.Title1
{mso-style-name:Title1;}
span.authors
{mso-style-name:authors;}
span.CommentTextChar
{mso-style-name:"Comment Text Char";
mso-style-link:"Comment Text";}
span.CommentSubjectChar
{mso-style-name:"Comment Subject Char";
mso-style-link:"Comment Subject";
font-weight:bold;}
span.Heading2Char
{mso-style-name:"Heading 2 Char";
mso-style-link:"Heading 2";
font-family:"Times New Roman","serif";
font-weight:bold;}
p.p1, li.p1, div.p1
{mso-style-name:p1;
margin-right:0in;
margin-left:0in;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
p.Default, li.Default, div.Default
{mso-style-name:Default;
margin:0in;
margin-bottom:.0001pt;
text-autospace:none;
font-size:12.0pt;
font-family:"Arial","sans-serif";
color:black;}
span.apple-converted-space
{mso-style-name:apple-converted-space;}
.MsoChpDefault
{font-size:10.0pt;}
/* Page Definitions */
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 89.85pt 64.9pt 89.85pt;}
div.WordSection1
{page:WordSection1;}
/* List Definitions */
ol
{margin-bottom:0in;}
ul
{margin-bottom:0in;}
-->
</style>
</head>
您不能强制浏览器显示 PDF,但您可以告诉它可以,如果它已经设置为显示的话。
您需要做的是使用您提到的 headers 提供文件。这告诉浏览器它正在加载的内容实际上是一个 PDF,如果它设置为打开 PDF,它就会打开。否则,可能会启动下载。
更具体地说,您需要将 headers 附加到 web-server 获取文件时给出的响应。实际应用程序可能会有很大差异,具体取决于您的 web-server 以及您提供文件的方式。
例如,如果文件是通过servlet提供的,你可以在servlet中设置content-type(java):
response.setContentType("application/pdf");
如果它是直接提供的(例如,您只是使用 链接到文件),那么您需要在服务器规模上进行设置。例如,在 tomcat 中,您在 web.xml 文件中这样设置:
<mime-mapping>
<extension>pdf</extension>
<mime-type>application/pdf</mime-type>
</mime-mapping>
这将服务器设置为任何带有 .pdf 扩展名的文件自动接收正确的 content-type。