gpt4 book ai didi

Base64 pdf with size > 3MB not loading with iframe(大小>3MB的Base64 pdf未随iFrame一起加载)

转载 作者:bug小助手 更新时间:2023-10-28 21:38:32 27 4
gpt4 key购买 nike



Base64 PDF iframe is not displaying on Chrome.

在Chrome上不显示Base64 PDF IFRAME。


PHP/HTML:

PHP/HTML:


<?php
$pdf = base64_encode(file_get_contents('https://freetestdata.com/wp-content/uploads/2022/11/Free_Test_Data_10.5MB_PDF.pdf'));
echo '<iframe src="data:application/pdf;base64,' . $pdf . '></iframe>';
?>

I tried <embed>, specifying width and height, and type="application/pdf" but it's not working.

我尝试了,指定宽度和高度,并键入=“application/pdf”,但它不起作用。


The file size is > 10MB I tried with 3MB file not working.

文件大小大于10MB,我尝试了3MB文件不工作。


It only worked with 39kb pdf.

它只适用于39kb的pdf。


更多回答

You have a syntax error in your HTML. It should be: echo '<iframe src="data:application/pdf;base64,' . $pdf . '"></iframe>';. Note the addition of the double quote. If your code really looks like this, why don't you just put the URL of the PDF in the SRC attribute of the IFRAME?

您的HTML中有一个语法错误。它应该是:ECHO‘’;。请注意添加了双引号。如果您的代码确实是这样的,为什么不直接将PDF的URL放在iframe的SRC属性中呢?

@KIKOSoftware it's a typo while copying the code but it's not the issue. I don't want to put the URL because I don't want to give the users direct access to the PDF file.

@KIKOSoftware这是一个错字,而复制代码,但它不是问题。我不想把网址,因为我不想让用户直接访问PDF文件。

I tested your code, after correcting the typo, and it worked for me. However, I agree with KJ that there might be a limit on the size of the data URL that a browser will accept. I'm using Firefox on Windows 11. I'll provide an answer with an alternative way to download the PDF which doesn't involve this limitation.

我测试了你的代码,在纠正了打字错误之后,它在我身上起作用了。然而,我同意KJ的观点,即浏览器接受的数据URL的大小可能是有限制的。我在Windows11上使用Firefox。我会提供一个不涉及这一限制的替代下载PDF的方法。

优秀答案推荐


To circumvent the data URL length limit you can create a PHP script on your server that will download the file for you:

要绕过数据URL长度限制,您可以在服务器上创建一个PHP脚本,该脚本将为您下载文件:


<?php

header('Content-type:application/pdf');
header('Content-Disposition:inline;filename="downloaded.pdf"');
readfile('https://freetestdata.com/wp-content/uploads/2022/11/Free_Test_Data_10.5MB_PDF.pdf');

See: readfile(). The headers are needed to tell the browser it's a PDF and what the file name is.

请参阅:Readfile()。需要标头来告诉浏览器这是一个PDF以及文件名是什么。


Now suppose you call this PHP script "download_pdf.php" you can use it like this:

现在假设您将此PHP脚本命名为“Download_pdf.php”,您可以这样使用它:


<?php

echo '<iframe src="download_pdf.php"></iframe>';

Now you're not saying anything about this, but if you want to be able to download more than one PDF you could work with an URL argument, like this:

现在您没有对此发表任何意见,但是如果您希望能够下载多个PDF,您可以使用URL参数,如下所示:


<?php

echo '<iframe src="download_pdf.php?fileId=3"></iframe>';

and then you need to modify the script like this:

然后,您需要像这样修改脚本:


<?php

switch (intval($_GET['fileId'] ?? 0)) {
case 0 : $url = 'https://example.com/404_error.pdf';
break;
case 1 : $url = '.....';
break;
case 2 : $url = '.....';
break;
case 3 : $url = 'https://freetestdata.com/wp-content/uploads/2022/11/Free_Test_Data_10.5MB_PDF.pdf';
break;
case 4 : $url = '.....';
break;
}

header('Content-type:application/pdf');
header('Content-Disposition:inline;filename="downloaded.pdf"');
readfile($url);

This is just an example. The URL could come from an array, or even from a database table.

这只是一个例子。URL可以来自数组,甚至可以来自数据库表。


更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com