PHP file_get_contents 用于动态生成 urls/pdf
PHP file_get_contents for dynamically generated urls/pdf
我想遍历一组 url 并创建一个 zip 文件。 url 是动态生成的 pdf files/invoices。该程序适用于物理上存在的文件,但对于动态生成的文件,它获得 html 源 code/page 而不是 pdf。
是否有替代方法使用:file_gets_contents ?.
下面是一段模拟代码:
// prepare array of URL's/files
$files = array(
"1" => "http://localhost/app/addon/test.pdf", //works
"2" => "http://localhost/app/addon/test2.pdf",//works
"3" => "http://localhost/app/index.php?section=flight-fatturazione&option=getPdf&tipo=invoice&id=6", //doesn't work
"4" => "http://localhost/app/index.php?section=flight-fatturazione&option=getPdf&tipo=invoice&id=4",
"5"=> "http://localhost/app/index.php?section=flight-fatturazione&option=getPdf&tipo=invoice&id=2"
);
// create new zip opbject
$zip = new ZipArchive();
// create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
// loop through url/file array
foreach($files as $file){
// download file
// this gets file contents, what to do when file is created on the fly
$download_file = file_get_contents($file);
// add it to the zip
$zip->addFromString(basename($file),$download_file);
}
// close zip
$zip->close();
// send the file to the browser as a download
header('Content-disposition: attachment; filename=download.zip');
header('Content-type: application/zip');
readfile($tmp_file);
目前,即使访问url直接生成pdf。
您正在将文件 3-5 保存为 .php-files。将它们重命名为 .pdf 有效吗?
如果如您所说,您收到 html 文件,它们是否包含 PHP-code 以生成它们? (例如,它是否返回 index.php 的代码?)很想知道它们是否真的得到处理。如果这些都不起作用,您是否需要特定的 cookie 或其他设置(会话?)才能发出获取 pdf 的请求?
折腾了半天,尝试了不同的方案,终于解决了这个问题。正如我在问题中提到的,我正在即时生成发票并尝试使用
获取内容
file_get_contents
通过传递生成的发票的 url。这在 zip 而不是 pdf 中返回了 html 页。有人建议使用 curl 并模拟 http 请求,但这并没有解决问题。所有这一切都是为了避免我的应用程序中的代码重复,但最后我没有选择。
因此,对于每张发票,我都会调用动态创建它的函数,然后使用 file_get_contents
获取内容
function createInvoiceContent($invoice_id){
require_once('tcpdf_include.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
/* Do a bunch of stuff to create the invoice */
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$invoice_content = $pdf->Output('invoice_name.pdf', 'I');
return $invoice_content;
}
我可以在需要的时候调用这个函数。
//get pdf invoice file
$invoice = file_get_contents(createInvoiceContent($invoice_id));
我想遍历一组 url 并创建一个 zip 文件。 url 是动态生成的 pdf files/invoices。该程序适用于物理上存在的文件,但对于动态生成的文件,它获得 html 源 code/page 而不是 pdf。
是否有替代方法使用:file_gets_contents ?.
下面是一段模拟代码:
// prepare array of URL's/files
$files = array(
"1" => "http://localhost/app/addon/test.pdf", //works
"2" => "http://localhost/app/addon/test2.pdf",//works
"3" => "http://localhost/app/index.php?section=flight-fatturazione&option=getPdf&tipo=invoice&id=6", //doesn't work
"4" => "http://localhost/app/index.php?section=flight-fatturazione&option=getPdf&tipo=invoice&id=4",
"5"=> "http://localhost/app/index.php?section=flight-fatturazione&option=getPdf&tipo=invoice&id=2"
);
// create new zip opbject
$zip = new ZipArchive();
// create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
// loop through url/file array
foreach($files as $file){
// download file
// this gets file contents, what to do when file is created on the fly
$download_file = file_get_contents($file);
// add it to the zip
$zip->addFromString(basename($file),$download_file);
}
// close zip
$zip->close();
// send the file to the browser as a download
header('Content-disposition: attachment; filename=download.zip');
header('Content-type: application/zip');
readfile($tmp_file);
目前,即使访问url直接生成pdf。
您正在将文件 3-5 保存为 .php-files。将它们重命名为 .pdf 有效吗?
如果如您所说,您收到 html 文件,它们是否包含 PHP-code 以生成它们? (例如,它是否返回 index.php 的代码?)很想知道它们是否真的得到处理。如果这些都不起作用,您是否需要特定的 cookie 或其他设置(会话?)才能发出获取 pdf 的请求?
折腾了半天,尝试了不同的方案,终于解决了这个问题。正如我在问题中提到的,我正在即时生成发票并尝试使用
获取内容file_get_contents
通过传递生成的发票的 url。这在 zip 而不是 pdf 中返回了 html 页。有人建议使用 curl 并模拟 http 请求,但这并没有解决问题。所有这一切都是为了避免我的应用程序中的代码重复,但最后我没有选择。
因此,对于每张发票,我都会调用动态创建它的函数,然后使用 file_get_contents
获取内容function createInvoiceContent($invoice_id){
require_once('tcpdf_include.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
/* Do a bunch of stuff to create the invoice */
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$invoice_content = $pdf->Output('invoice_name.pdf', 'I');
return $invoice_content;
}
我可以在需要的时候调用这个函数。
//get pdf invoice file
$invoice = file_get_contents(createInvoiceContent($invoice_id));