简体   繁体   中英

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

I hate lengthy questions,but as i am new to PHP im writing full code.Please Bare me.
I'm trying to generate a Invoice in PHP in Xampp which contains "tinymce text editor" in the form ,which im using to edit content. Here is the create.php page

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

The content i am editing in Editor is a table with nxn cells, but the data is saving in the database in HTML code.

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

when i am retrieving the data into read.php,Output is coming as expected. Image of read.php

But when im generating the page into pdf using fpdf, the output is like this, not as expected

I tried both html_entity_decode and strip_tags also html_entity_decode(strip_tags($data['content']))

I just want the pdf to be same as read.php.. Just help me,in what i need to concentrate to get what i want , Thanks in Advance.

When trying to convert html to pdf I found wkhtml to be the best free tool available. It can be found at http://wkhtmltopdf.org/ or installed via linux package manager (usually via apt-get install wkhtmltopdf ) on many distributions. The benefit of this tool is that it uses the webkit browser engine to really render the webpage like a browser would do, including html and css. This implies that you can use all available css styling to format your invoice.

If your php interpreter can access and execute the wkhtmltopdf file than conversion is as simple as this:

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

In php you ccan do this by using exec()

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

Alternatives:

  1. Use a library like fpdf for pdf creation. You'll have to create plain text values from your html on your own. This will give you flexibility but you have to handle the data completely on your own.
  2. There are some libraries out there that promise to convert html directly to pdf. All of those libraries will only implement a subset of html/css so you will have to adopt your styling to meet the capabilities of the library in use.

actual this is not a complete answer. Just sharing an idea: html page

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

In a js file get content from div and pass it to the place where PDF is generating:

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

Get the passed html data from js:

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

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