How to Save Dompdf Generated Content to File

how to save DOMPDF generated content to file?

I have just used dompdf and the code was a little different but it worked.

Here it is:

require_once("./pdf/dompdf_config.inc.php");
$files = glob("./pdf/include/*.php");
foreach($files as $file) include_once($file);

$html =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$output = $dompdf->output();
file_put_contents('Brochure.pdf', $output);

Only difference here is that all of the files in the include directory are included.

Other than that my only suggestion would be to specify a full directory path for writing the file rather than just the filename.

PHP: Automatically Saving a dynamic PDF to the remote server using DOMPDF

You can do one thing like below which i am doing for my application. First create a folder in your server under the root directory for example. Then change read write permissions to that folder using chmod command.

Then get all the code in $html string.

$dompdf->load_html($html);    
$dompdf->render();
$pdf = $dompdf->output();
$file_location = $_SERVER['DOCUMENT_ROOT']."app_folder_name/pdfReports/".$pdf_name.".pdf";
file_put_contents($file_location,$pdf);

Where pdfReports is the folder which i created to save all the pdf's. You can change to your folder name.

Dompdf creates new output pdf-document, I need to output content in one file, how to fix it?

$dompdf->stream(); outputs to browser so if you don't want that happening remove that line.

$dompdf->output(); creates the PDF as a string so you could save it as a file:

$output = $dompdf->output();
file_put_contents(get_template_directory() . '/pdf-test.pdf', $output);

Saving DOMPDF to specified directory and then sending as email

The path .../third_party/pdf/ is probably a directory and not a file. PHP can't save your data as a directory. Specify a filename as first parameter of file_put_contents(), e.g. third_party/pdf/my_document.pdf.

Save and download PDF

Why dont you swap them round create the pdf first as a file and then stream the created file back ?

$file_to_save = '/home/stsnew/public_html/pdf/file.pdf';
//save the pdf file on the server
file_put_contents($file_to_save, $dompdf->output());
//print the pdf file to the screen for saving
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="file.pdf"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file_to_save));
header('Accept-Ranges: bytes');
readfile($file_to_save);

I generated the multiple pdf from dompdf but how can I save these files locally

You can use the method output() to get the PDF content and save into a file using file_get_contents():

file_put_contents('/path/to/file.pdf', $dompdf->output());

EDIT after question changed :

for ($i=0; $i<count($sheetData); $i++) 
{
$dompdf = new DOMPDF();
$dompdf->load_html($html);

$dompdf->render();
file_put_contents('pdf' . $i . '.pdf', $dompdf->output());
$dompdf->stream();
}

But it seems strange to generate N times, the same PDF.



Related Topics



Leave a reply



Submit