PDFObject:如何使用同一文件的不同副本重新加载容器?

时间:2022-10-07 16:41:25

I have a button that generates a PDF, then reloads the PDF in the browser by replacing the container it's in with itself, but with a different link. For some reason, the new PDF doesn't show up, the old one is there still no matter how many times it is refreshed. I'm not using an iframe or an object/embed method, I'm using http://pdfobject.com/

我有一个生成PDF的按钮,然后通过用自己替换容器来重新加载PDF,但使用不同的链接。出于某种原因,新的PDF没有出现,旧的PDF仍然无论刷新了多少次。我没有使用iframe或object / embed方法,我正在使用http://pdfobject.com/

My html pdfobject container:

我的html pdfobject容器:

<div id="pdfContainer">
    <div id="pdfSample"></div>
</div>

My Javascript to initiate the src upon window load:

我的Javascript在窗口加载时启动src:

<script type="text/javascript">
    $(function () {
        PDFObject.embed("myInitialPDF.pdf", "#pdfSample");
    })
</script>

My AJAX call to the PDF-creation/container-refresh:

我的AJAX调用PDF创建/容器刷新:

$('#genPDF').click(function () {
    $.ajax({
        url: "mergePDF.php",
        data: str,
        cache: false,
        success: function (result) {
            $("#pdfobject").attr("src", "newPDF.pdf");
            var container = document.getElementById("pdfContainer");
            var content = container.innerHTML;
            container.innerHTML = content;
        }
    });
});

Everytime the button with ID genPDF is clicked, a new PDF is generated by the name "newPDF.pdf" and the container should replace the previously assigned src path with the new source path of the new PDF (which is the same name as before). I don't understand if it's cache that's preventing this or not. Am I missing something?

每次单击ID为genPDF的按钮时,将生成一个名为“newPDF.pdf”的新PDF,并且容器应使用新PDF的新源路径(与以前名称相同)替换先前分配的src路径。我不明白它的缓存是否阻止了这一点。我错过了什么吗?

2 个解决方案

#1


1  

You specified cache: false this means that success: function (result) { is passed a not-cached result. You then proceed to never use it and instead change the src of an object to what it originally was, so the results of fetching that src url are cached by the browser because they were fetched before. Even if it's not cached, the browser will probably never fetch the file again because it never perceived a change of value in src.

你指定了cache:false这意味着成功:function(result){传递了一个未缓存的结果。然后继续使用它,而不是将对象的src更改为它最初的内容,因此获取该src url的结果由浏览器缓存,因为它们之前已被提取。即使它没有被缓存,浏览器也可能永远不会再次获取文件,因为它从未感知到src中的值更改。

Still with me?

还在我这儿?

What you could do is add a random number to the end of the url so that it's a new url and the browser will fetch the file again because it doesnt know if newPDF.pdf?number=random1 is or isn't the same as newPDF.pdf?number=random2. Even better, you can add a number that you know will be different every time you try it, like the current time in seconds.

你可以做的是在网址的末尾添加一个随机数,这样它就是一个新网址,浏览器会再次获取文件,因为它不知道newPDF.pdf?number = random1是否与newPDF相同.PDF?数= random2。更好的是,您可以添加一个您知道每次尝试时都会有所不同的数字,例如以秒为单位的当前时间。

Here's your code again, with a minor adjustment:

这是你的代码,稍作调整:

$('#genPDF').click(function () {
    $.ajax({
        url: "mergePDF.php",
        data: str,
        cache: false,
        success: function (result) {
            var t = new Date.getTime();
            $("#pdfobject").attr("src", "newPDF.pdf?"+t);
            var container = document.getElementById("pdfContainer");
            var content = container.innerHTML;
            container.innerHTML = content;
        }
    });
});

Something important I'd like to add: This isn't a good enough solution, even though it answers your question. A better solution would be to generate a file named after the timestamp (with the pdf extension of course) and return that as result and use it in $("#pdfobject").attr("src", result);. Make sure you clean up any files that are named with a timestamp that is really old, like more than an hour, a day or a week depending on what you think is right. This will ensure that nobody is fighting over the filename by triggering a new PDF creation at almost the same time, but also make sure you don't have a massive archive of obsolete PDF files.

我想补充一些重要内容:这不是一个足够好的解决方案,即使它回答了你的问题。更好的解决方案是生成一个以时间戳命名的文件(当然使用pdf扩展名)并将其作为结果返回并在$(“#pdfobject”)。attr(“src”,result);中使用它。确保清理所有使用时间戳命名的文件,例如超过一小时,一天或一周,具体取决于您的想法。这将确保没有人通过几乎同时触发新的PDF创建来争夺文件名,但也要确保没有大量过时的PDF文件存档。

#2


1  

I had the same problem. I set it by disabling the cache for pdf files in .htaccess add this:

我有同样的问题。我通过在.htaccess中禁用pdf文件的缓存来设置它添加:

<FilesMatch "\.(txt|pdf)$">
  ExpiresActive On
  ExpiresDefault A1

  Header set Cache-Control "max-age=0, no-store, must-revalidate"
</FilesMatch>

#1


1  

You specified cache: false this means that success: function (result) { is passed a not-cached result. You then proceed to never use it and instead change the src of an object to what it originally was, so the results of fetching that src url are cached by the browser because they were fetched before. Even if it's not cached, the browser will probably never fetch the file again because it never perceived a change of value in src.

你指定了cache:false这意味着成功:function(result){传递了一个未缓存的结果。然后继续使用它,而不是将对象的src更改为它最初的内容,因此获取该src url的结果由浏览器缓存,因为它们之前已被提取。即使它没有被缓存,浏览器也可能永远不会再次获取文件,因为它从未感知到src中的值更改。

Still with me?

还在我这儿?

What you could do is add a random number to the end of the url so that it's a new url and the browser will fetch the file again because it doesnt know if newPDF.pdf?number=random1 is or isn't the same as newPDF.pdf?number=random2. Even better, you can add a number that you know will be different every time you try it, like the current time in seconds.

你可以做的是在网址的末尾添加一个随机数,这样它就是一个新网址,浏览器会再次获取文件,因为它不知道newPDF.pdf?number = random1是否与newPDF相同.PDF?数= random2。更好的是,您可以添加一个您知道每次尝试时都会有所不同的数字,例如以秒为单位的当前时间。

Here's your code again, with a minor adjustment:

这是你的代码,稍作调整:

$('#genPDF').click(function () {
    $.ajax({
        url: "mergePDF.php",
        data: str,
        cache: false,
        success: function (result) {
            var t = new Date.getTime();
            $("#pdfobject").attr("src", "newPDF.pdf?"+t);
            var container = document.getElementById("pdfContainer");
            var content = container.innerHTML;
            container.innerHTML = content;
        }
    });
});

Something important I'd like to add: This isn't a good enough solution, even though it answers your question. A better solution would be to generate a file named after the timestamp (with the pdf extension of course) and return that as result and use it in $("#pdfobject").attr("src", result);. Make sure you clean up any files that are named with a timestamp that is really old, like more than an hour, a day or a week depending on what you think is right. This will ensure that nobody is fighting over the filename by triggering a new PDF creation at almost the same time, but also make sure you don't have a massive archive of obsolete PDF files.

我想补充一些重要内容:这不是一个足够好的解决方案,即使它回答了你的问题。更好的解决方案是生成一个以时间戳命名的文件(当然使用pdf扩展名)并将其作为结果返回并在$(“#pdfobject”)。attr(“src”,result);中使用它。确保清理所有使用时间戳命名的文件,例如超过一小时,一天或一周,具体取决于您的想法。这将确保没有人通过几乎同时触发新的PDF创建来争夺文件名,但也要确保没有大量过时的PDF文件存档。

#2


1  

I had the same problem. I set it by disabling the cache for pdf files in .htaccess add this:

我有同样的问题。我通过在.htaccess中禁用pdf文件的缓存来设置它添加:

<FilesMatch "\.(txt|pdf)$">
  ExpiresActive On
  ExpiresDefault A1

  Header set Cache-Control "max-age=0, no-store, must-revalidate"
</FilesMatch>