簡體   English   中英

無法打開使用 dompdf 生成的 pdf 文件

[英]cannot open pdf file generated using dompdf

我正在嘗試使用 dompdf 從 smarty 模板生成 pdf 文件:代碼如下:-

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");

生成了一個 pdf 文件,但是當我嘗試打開 pdf 文件時,顯示如下錯誤消息“Acrobat 無法打開‘sample.pdf’,因為它要么不受支持的文件類型,要么因為文件已損壞”HTML 頁面是當我嘗試正確顯示時

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

所以請幫我解決這個錯誤...提前致謝

使用ob_end_clean(); 在調用$dompdf->stream(); .

我也遇到了同樣的錯誤,但是當我在$dompdf->stream()之前使用ob_end_clean()$dompdf->stream()我有用。

下面是我的代碼。

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

是的,這是dompdf的問題。 但我能夠克服這個問題。 我創建了一個用於創建 pdf 的函數。 檢查以下功能:-

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); 

}

注意:- 您需要將生成的 pdf 作為字符串,然后將其保存為 pdf 文件。

編輯:-您可以從上述功能中刪除以下部分:-

    $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));

以上代碼用於處理多個頁面標題。

它對我有用,最后我的代碼看起來像:

$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);

我的 dompdf 創建的 pdf 有問題我在記事本++中打開 pdf 文件我發現以下 php 錯誤與 dompdf

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

發生這種情況是由於舊版本的 dompdf 在更新 dompdf 庫后無法支持 php 7 版本,問題得到解決。

在此處下載最新的 dompdf 庫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM