简体   繁体   English

如何在PDF文档之前显示加载屏幕

[英]How to show loading screen before PDF document

In our application we have links to dynamically generated PDF documents. 在我们的应用程序中,我们链接到动态生成的PDF文档。 The links look something like this host/22-5/file_3136.pdf so to the browser it seems like a static pdf document. 链接看起来像这个host / 22-5 / file_3136.pdf,所以对浏览器来说它看起来像一个静态的pdf文档。 When link is clicked it opens a new window. 单击链接时,将打开一个新窗口。 That window receives PDF document only (no HTML) with headers like: 该窗口仅接收带有以下标题的PDF文档(无HTML):

Content-Disposition: inline; filename=file_3136.pdf
Content-Type: application/pdf

We want users to be able to see the PDF in the browser if PDF plug-in is installed and to be able save the document with correct filename. 我们希望用户能够在安装了PDF插件的情况下在浏览器中查看PDF,并且能够使用正确的文件名保存文档。

Now we want to add a loading screen that would be shown while the PDF is being generated. 现在我们要添加一个加载屏幕,该屏幕将在生成PDF时显示。 Whats the best way to do that, while retaining the current functionality. 什么是最好的方法,同时保留当前的功能。

One option would be to show the loading screen and then to redirect to PDF when generation is complete. 一种选择是显示加载屏幕,然后在生成完成时重定向到PDF。 This would require me to retain the PDF on the server for some time. 这需要我将PDF保留在服务器上一段时间。 Currently they are being deleted as soon as the response is sent. 目前,一旦发送响应,它们就会被删除。

Another option is to send some HTML and javascript (to show the loading page) with <embed> , <iframe> or <object> tag that points to the pdf on the server. 另一种选择是使用<embed><iframe><object>标记发送一些HTML和javascript(以显示加载页面),该标记指向服务器上的pdf。

What the best approach? 什么是最好的方法? What works with most browsers? 什么适用于大多数浏览器?

On download sites, I often see an additional (small) window pop up. 在下载网站上,我经常会看到另一个(小)窗口弹出。 I believe that window acts as a "choreographer" to control what displays on the main page while also firing off a redirect to the download file. 我相信该窗口充当“编舞者”来控制主页面上显示的内容,同时还启动重定向到下载文件。

HTML redirects. HTML重定向。 You create a page that redirects "to itself" every few seconds. 您创建一个每隔几秒重定向“自身”的页面。 When the PDF is done, you generate a redirect to that instead. PDF完成后,您将生成重定向到该PDF。

To actually preload the PDF file so that the impression of instant loading is given. 实际上预加载PDF文件,以便给出即时加载的印象。 in the page prior to the pdf linked page add a preload script: 在pdf链接页面之前的页面中添加预加载脚本:

<img id="pdfLoader" src="preloader.jpg"/>

You can get a preloader image from ajaxload.info 您可以从ajaxload.info获取预加载器映像

<script language="javascript" type="text/javascript">
//<![CDATA[
<!-- 
var pdfLoader = document.getElementById("pdfLoader");
pdfLoader.src = "http://mysite.com/mypdf.pdf";
//-->
//]]>
</script>

The code above placed between and in the page containing the link to the .pdf file (or html file with the .pdf in it) instructs the browser to download the pdf file to the browser cache, but just leave it there. 上面的代码放在包含.pdf文件链接的页面之间和页面中(或带有.pdf的html文件),指示浏览器将pdf文件下载到浏览器缓存中,但只需将其保留在那里。 (filetype is irelevant image() is convenient, use the same script for any filetype as it is not ever going to be rendered) the download happens after the page is fully rendered so does not delay the current page. (filetype是irelevant image()很方便,对于任何文件类型使用相同的脚本,因为它不会被渲染)下载发生在页面完全呈现之后,因此不会延迟当前页面。 on clicking the link to the .pdf file (or html page) the browser finds the .pdf in the browser cache and does not download it, but displays from the cache, at apparently blinding download speeds. 在点击指向.pdf文件(或html页面)的链接时,浏览器会在浏览器缓存中找到.pdf并且不会下载它,而是以缓慢的下载速度从缓存中显示。

In browsers with javascript disabled, the function degrades gracefully 在禁用javascript的浏览器中,该功能会正常降级

If you linearize the PDf you can also display the first page very quickly. 如果线性化PDf,您还可以非常快速地显示第一页。 I wrote 2 articles introducing linearized PDF at http://www.jpedal.org/PDFblog/2010/11/do-i-have-to-download-the-whole-pdf-if-i-view-it-across-the-internet/ and http://www.jpedal.org/PDFblog/2010/02/linearized-pdf-files/ 我在http://www.jpedal.org/PDFblog/2010/11/do-i-have-to-download-the-whole-pdf-if-i-view-it-across-上写了两篇介绍线性化PDF的文章。 the-internet /http://www.jpedal.org/PDFblog/2010/02/linearized-pdf-files/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM