简体   繁体   English

在Javascript中创建PDF预加载器

[英]Creating a PDF preloader in Javascript

I have a website that displays a pdf file embedded in the browser. 我有一个网站,显示嵌入浏览器的pdf文件。 The code for it is below: 它的代码如下:

document.getElementById("bigone").innerHTML = 
'<object type="application/pdf" data="\\PDF\\somefile.pdf" width=100% height="720px"></object>';

Basically using innerHTML to insert an object tag with a PDF as its data ("bigone" is a table cell). 基本上使用innerHTML插入带有PDF作为其数据的对象标记(“bigone”是表格单元格)。 This works perfectly fine, but sometimes the PDF's are large at take time to load, and the user is faced with a blank screen. 这完全正常,但有时PDF需要花费很长时间才能加载,并且用户面临着一个空白屏幕。 I want to insert an image preloader (like the text "LOADING...") while the pdf file downloads in the background. 我想在后台下载pdf文件时插入图像预加载器(如文本“LOADING ...”)。

I've using the Image object in JS but have had no success. 我在JS中使用了Image对象但没有成功。

document.getElementById("bigone").innerHTML = '<img src="gradients\\loading.png" />'
var pdf = new Image();
pdf.src = "\\PDF\\somefile.pdf";
pdf.onLoad = function() {
document.getElementById("bigone").innerHTML = '<object type="application/pdf" data="\\PDF\\somefile.pdf" width=100% height="720px"></object>';
}

This doesn't work, the preloader image appears, but isn't replaced with the embedded PDF. 这不起作用,预加载器图像出现,但不会被嵌入的PDF替换。 I placed pdf.onLoad inside an alert and it returned null. 我将pdf.onLoad放在一个警报中,它返回null。 Is there anything wrong with my code, or any ideas anyone has to offer? 我的代码或任何人提供的任何想法有什么问题吗? Thanks. 谢谢。

You can't load a PDF file into an Image object... an Image object is for images. 您无法将PDF文件加载到Image对象中... Image对象用于图像。

What I'd do is create a DIV element in the HTML that is a container for the PDF file that's going to be in the OBJECT element. 我要做的是在HTML中创建一个DIV元素,它是PDF文件的容器,它将在OBJECT元素中。

Then I could set the background-image style attribute to be that loading picture so that whilst the PDF is loading people can see the "loading" image in the background, and once the PDF has loaded it will appear on top of the loading image so people can't see that any more. 然后我可以将背景图像样式属性设置为加载图片,以便在加载PDF时人们可以在后台看到“加载”图像,一旦PDF加载,它将出现在加载图像的顶部,这样人们再也看不到了。

<td id="bigone">
    <div id="pdf_container" style="background-image: url('\\gradients\\loading.png'); background-repeat: none; background-position: 50% 50%; height: 720px; width: 100%;">
    </div>
</td>

<script type="text/javascript">
    document.getElementById("pdf_container").innerHTML = '<object type="application/pdf" data="\\PDF\\somefile.pdf" width=100% height="720px"></object>';
</script>

Scrap that, you could just apply that background-image to the table cell instead of having to make the new DIV element too. 废话,您可以将该背景图像应用于表格单元格,而不必制作新的DIV元素。

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

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