如何用codeigniter将dompdf生成的内容保存到文件中?

os8fio9y  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(119)

我有一个页面中有一个可打印的pdf,我想打印的pdf将被保存在一个文件夹中的文件形式请帮助我!

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename='', $stream=TRUE, $paper = 'Letter', $orientation = 'portrait') 
{

    require_once("dompdf/dompdf_config.inc.php");
    
    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->set_paper($paper, $orientation);

    $dompdf->render();
    if ($stream) {
        $dompdf->stream($filename.'.pdf', array("Attachment" => 0));
    } else {
        return $dompdf->output(); 
    }
}
fd3cxomn

fd3cxomn1#

检查DOMPDF源,我们可以很快发现,$dompdf->stream()不支持file://协议,因此只能用于流PDF到浏览器。
https://github.com/iamfiscus/Codeigniter-DOMPDF/blob/575f68e54d2c37b50a99fc7e700d136323cd2fb7/third_party/dompdf/lib/class.pdf.php#L3041
但是,$dompdf->output()会将整个PDF作为字符串返回,因此您只需将字符串保存到文件中。
https://github.com/iamfiscus/Codeigniter-DOMPDF/blob/575f68e54d2c37b50a99fc7e700d136323cd2fb7/third_party/dompdf/lib/class.pdf.php#L1884

file_put_contents('my.pdf', $dompdf->output());
afdcj2ne

afdcj2ne2#

步骤1:使用Dompdf输出()获取PDF内容。
步骤2:使用PHP file_put_contents()函数在PDF文件中插入内容。

$dompdf->render();
$output = $dompdf->output();

$fileName = 'myDocument.pdf';
$path = "pdf_files/".$fileName;

file_put_contents($path, $output);

相关问题