Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

HTML/CSS to PDF - HTML stored in database and not display on page

I'm doing an e-commerce website. I'm have to manage bill for all orders.

I'm doing that like this :

  1. When the user paid his order, i generate the HTML code for the bill and i store it into my database. (I used this way because i want static bill with the user's informations at the moment of the payment).

  2. In the user's account, the user can see his order and in the order's detail, there is a button "Download bill". When this button is clicked, i want to generate the PDF (downloaded by the browser) with HTML/CSS code stored in the database.

I'm looking for a good library which support the CSS (in PHP, because in Javascript, the PDF is generate from a div visible in the screen). I tried : HTML2PDF, FPDF but never worked on my website "Some data has already been output, can't send PDF". When, i run the code in a new project, with only an index.php file, it works.

Does someone have an idea ?

Thanks !!

Little example that work in a simple index.php file and didn't work on my website :

 <?php
    require_once "lib/html2pdf/html2pdf.php"; // library doesn't support the CSS
    ob_start();?>

    <page backtop="10mm" backleft="10mm" backright="10mm" backbottom="10mm">
    <div style="width:1170px;">
        <table style="margin-top:25px;background-color: transparent;border-collapse: collapse;border-spacing: 0;float:right;width:50%;">
            <tr>
                <td style="text-align:right;">Total HTVA</td>
            </tr>

            <tr>
                <td style="font-size:18px;text-align:right;padding-top:10px;"><b>Total</b></td>
            </tr>
            <tr>
                <td style="text-align:right;padding-top:10px;"><b>To pay</b></td>
                <td style="text-align:right;padding-top:10px;"><b>0,00 €</b></td>
            </tr>
        </table>
    </div>
    </page>

    <?php
        $content = ob_get_clean();
        try {
            $pdf = new HTML2PDF("p","A4","fr");
            $pdf->pdf->SetAuthor('Magica Feeria Fantasy');
            $pdf->pdf->SetTitle('Formulaire de rétractation');
            $pdf->writeHTML($content);
            $file = $pdf->Output('formulaireRetractation.pdf');
        } catch (HTML2PDF_exception $e) {
            var_dump($e);
        }

    ?>

Comments