简体   繁体   中英

cannot open pdf file generated using dompdf

i am trying to generate a pdf file from smarty template using dompdf:code is below:-

require_once('dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
$dompdf->load_html($smarty->fetch(CURRENT_TEMPLATE.'/module/shopping_cart.html'));
$dompdf->render();
$dompdf->stream("sample.pdf");

a pdf file is generated,but when i try to open the pdf file a error message is displaying as below "Acrobat cannot open the 'sample.pdf' because it is either not supported file type or because file has been damaged" HTML page is diplaying correctly when i try to

echo $smarty->fetch(CURRENT_TEMPLATE.'/module/shopping_cart.html')

so please help me to solve this error...thanks in advance

Use ob_end_clean(); before calling $dompdf->stream(); .

I also got same error but when I use ob_end_clean() before $dompdf->stream() it worked for me.

Below is my code.

$dompdf = new DOMPDF();
$dompdf->load_html($html); 
$dompdf->render();    
$pdf = $dompdf->output();
$invnoabc = 'Bokkinglist.pdf';
ob_end_clean();
$dompdf->stream($invnoabc);
exit;

yes this is an problem of dompdf. But I am able to overcome from this problem. I created a function for pdf creation. check below function:-

function pdf_create($html, $filename='', $stream=TRUE) 
{
    require_once("dompdf/dompdf_config.inc.php");
    $savein = 'uploads/policy_doc/';
    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->render();
    $canvas = $dompdf->get_canvas();
    $font = Font_Metrics::get_font("arial", "normal","12px");

    // the same call as in my previous example
    $canvas->page_text(540, 773, "Page {PAGE_NUM} of {PAGE_COUNT}",
                   $font, 6, array(0,0,0));

    $pdf = $dompdf->output();      // gets the PDF as a string

    file_put_contents($savein.str_replace("/","-",$filename), $pdf);    // save the pdf file on server
    unset($html);
    unset($dompdf); 

}

Note :- You need to get generated pdf as string then save it to pdf file.

EDIT :- You may delete the below part from above function:-

    $canvas = $dompdf->get_canvas();
    $font = Font_Metrics::get_font("arial", "normal","12px");

    // the same call as in my previous example
    $canvas->page_text(540, 773, "Page {PAGE_NUM} of {PAGE_COUNT}",
                   $font, 6, array(0,0,0));

this above code for handling multiple pages header.

it works to me, at end my code looks like:

$html =
  '<html><body>'.
  '<p>Put your html here, or generate it with your favourite '.
  'templating system.</p>'.
  '</body></html>';
function pdf_create($html, $filename='', $stream=TRUE) 
{
    require_once('scripts/vendor/dompdf/dompdf/dompdf_config.inc.php');
    $savein = '';
    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->render();
    $canvas = $dompdf->get_canvas();
    $font = Font_Metrics::get_font("arial", "normal","12px");

    /*// the same call as in my previous example
    $canvas->page_text(540, 773, "Page {PAGE_NUM} of {PAGE_COUNT}",
                   $font, 6, array(0,0,0));*/

    $pdf = $dompdf->output();      // gets the PDF as a string

    file_put_contents("arquivo.pdf",$pdf);  // save the pdf file on server

    unset($html);
    unset($dompdf); 

}

pdf_create($html);

I having issue with my pdf created by dompdf I open pdf file in notepad++ i found the below php error with dompdf

Message:  "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"

This happened due to old version of dompdf that could not be supported php 7 version after updating dompdf library the issue get resolved.

download latest dompdf library here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM