Home > Mobile >  How to save dompdf generated content to file with codeigniter?
How to save dompdf generated content to file with codeigniter?

Time:04-05

I have a page in which there is a printable pdf, I want the printed pdf to be saved in a folder in the form of a file Please help me!

<?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(); 
    }
}

CodePudding user response:

Checking DOMPDF sources we can quickly find, that $dompdf->stream() does not support file:// protocol and thus can only be used for streaming PDF to browser.

https://github.com/iamfiscus/Codeigniter-DOMPDF/blob/575f68e54d2c37b50a99fc7e700d136323cd2fb7/third_party/dompdf/lib/class.pdf.php#L3041

However, $dompdf->output() will return whole PDF as a string, so all you need is to save a string to file.

https://github.com/iamfiscus/Codeigniter-DOMPDF/blob/575f68e54d2c37b50a99fc7e700d136323cd2fb7/third_party/dompdf/lib/class.pdf.php#L1884

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

CodePudding user response:

Step1: Use the Dompdf output() to get the PDF content.

Step2: Use PHP file_put_contents() function to insert the content in your PDF file.

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

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

file_put_contents($path, $output);
  • Related