首页 > 解决方案 > mPDF 传递带有参数的 URL 以创建 PDF 文件

问题描述

我创建了一个 pdf.php

<?php 
include("mpdf60/mpdf.php");

$url = $_GET['url'];

ob_start(); 
include($url);
$html = ob_get_clean();
$mpdf=new mPDF('utf-8', 'A3-L'); 
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
?>

但是当传递参数示例时:

<?php

$url = 'gerencial_consad.php?mes='.$mes.'&ano='.$ano.'&menu=N';

?>

<a href="pdf.php?url=<?php echo $url; ?>">[ Gerar PDF ]</a>

创建文件 PDF 时出现错误:

警告:include(gerencial_consad.php?mes=01):未能打开流:结果在 C:\wamp64\www\codforv2\pdf.php 中的第 7 行调用堆栈中太大

时间记忆功能位置

1 0.0005 240264 {main}( ) ...\pdf.php: ( ! ) 警告:include(): 未能打开 'gerencial_consad.php?mes=01' 以包含 (include_path='.;C:\php\pear ') 在 C:\wamp64\www\codforv2\pdf.php 第 7 行调用堆栈

时间记忆功能位置

1 0.0005 240264 {main}( ) ...\pdf.php:

请帮帮我!!!!

谢谢!

标签: phpurlgetparameter-passingmpdf

解决方案


您不想包含 URL,而是想使用 HTTP 客户端下载 URL 的内容。

最简单的方法,可能受限于allow_url_fopenini 设置,是使用file_get_contents函数。

您需要将整个 URL 传递给函数:

$url = $_GET['url']; // http://example.com/
$html = file_get_contents($url);
$mpdf->WriteHTML($html);

警告:通过使用未经过滤和未经处理的用户输入($_GET['url']直接),您将在应用程序中打开一个安全漏洞。始终将输入限制为您知道是安全的内容。

您最好只从请求中获取 URL 的参数并在内部组合最终 URL。


推荐阅读