簡體   English   中英

如何在PHP的fpdf輸出中將數據庫中的html代碼轉換為純文本?

[英]How to convert html code in database to plain-text in fpdf output in PHP?

我討厭冗長的問題,但是由於我是PHP新手 ,所以我正在編寫完整的代碼。請裸露我。
我正在嘗試在Xampp中的PHP中生成一個發票,發票包含表單形式的“ tinymce文本編輯器”,即時通訊用於編輯內容。 這是create.php頁面

  <script src='js/tinymce.min.js'></script>
  <script>
  tinymce.init({
      selector: '#mytextarea',  
        plugins: [
                  'advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker',
                  'searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking',
                  'save table contextmenu directionality emoticons template paste textcolor'
                ],

      a_plugin_option: true,
      a_configuration_option: 400
    });

  </script>

我在編輯器中編輯的內容是帶有nxn個單元格的表,但是數據以HTML代碼保存在數據庫中。

<table>
   <tbody>
       <tr>
        <td>Sl.NO</td>
        <td>Some Text</td>
        <td>Some more TExt</td>
        </tr>
    <tr>
        <td>1</td>
        <td>Some Text</td>
        <td>Some more TExt</td>
        </tr>
        </tbody>
        </table>

當我將數據檢索到read.php中時,輸出將按預期進行。 read.php的圖像

但是當我使用fpdf將頁面生成為pdf時,輸出是這樣的, 而不是預期的

我同時嘗試了html_entity_decode和strip_tags html_entity_decode(strip_tags($data['content']))

我只希望pdf與read.php相同。只需幫助我,我需要集中精力得到想要的東西,在此先感謝。

當嘗試將html轉換為pdf時,我發現wkhtml是可用的最佳免費工具。 可以在http://wkhtmltopdf.org/上找到它,也可以通過linux軟件包管理器(通常通過apt-get install wkhtmltopdf )在許多發行版中進行apt-get install wkhtmltopdf 此工具的好處是,它使用webkit瀏覽器引擎像瀏覽器一樣真正地渲染網頁,包括html和css。 這意味着您可以使用所有可用的CSS樣式來格式化發票。

如果您的php解釋器可以訪問和執行wkhtmltopdf文件,那么轉換就這么簡單:

wkhtmltopdf http://your.domain/read.php /my/path/invoice.pdf

在PHP中,您可以使用exec()來做到這一點

$url = escapeshellarg('http://your.domain/read.php');
$filePath = escapeshellarg('/my/path/invoice.pdf');
exec('wkhtmltopdf ' . $url . ' ' . $filePath);

備擇方案:

  1. 使用fpdf類的fpdf來創建pdf。 您必須自己從html創建純文本值。 這將為您提供靈活性,但您必須完全自行處理數據。
  2. 有一些庫可以將html直接轉換為pdf。 所有這些庫將僅實現html / css的子集,因此您將必須采用樣式來滿足使用中的庫的功能。

實際上這不是一個完整的答案。 只是分享一個想法:html頁面

<div id="htmltable">
    <table>....</table>
</div>

在一個js文件中,從div獲取內容並將其傳遞到PDF生成的位置:

htmltable = $( "#htmltable" ).html();

從js獲取傳遞的html數據:

$pdf=new PDF_HTML();
$pdf->AliasNbPages();
$pdf->SetAutoPageBreak(true, 15);
$pdf->AddPage();
$pdf->SetFont('Arial','B',14);
$htmlTable = '<div id="htmltable"><table>....</table></div>';
$pdf->WriteHTML2($htmlTable);
$pdf->Output(); 

暫無
暫無

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

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