Jara Skuder
Jara Skuder

Reputation: 91

Export MySql table content to PDF with php

Does any one had this? Is there any way to avoid using libraries? I tried this but i think it needs some kind of libraries

This is my controller php:

if ($this->_request->isPost() && $is_pdf) {

        $file3 = 'file.pdf';
        $pdf = pdf_new();
        pdf_open_file($pdf, "php://output");
        pdf_begin_page($pdf, 595, 842);
        $arial = pdf_findfont($pdf, "Arial", "host", 1); pdf_setfont($pdf, $arial, 10);
        $data = $model->fetchAll();


        foreach ($data as $fields) {


            fputcsv($pdf, $fields->toArray());
        }
        pdf_end_page($pdf);
        pdf_close($pdf);

        if ($file3 !== false) {

            header('Content-type: application/pdf');
            header('Content-Disposition: attachment; filename="' . $file3 . '"');
            exit;
        }
    }

And my HTML form with button and hidden input:

 <form method="post">
            <input type="hidden" name="table" value="<?php echo $this->table ?>" />
            <button type="submit" class="btn" name="csv" value="csv"><?php echo Core_Locale::translate('CSV')?></button>
            <button type="submit" class="btn" name="xls" value="xls"><?php echo Core_Locale::translate('Excel')?></button>
            <button type="submit" class="btn" name="pdf" value="pdf"><?php echo Core_Locale::translate('PDF')?></button>
        </form>

Upvotes: 1

Views: 3929

Answers (1)

lmarcelocc
lmarcelocc

Reputation: 1361

Your code seems to use PECL pdflib library. That need to be installed/enabled in your php.

If i'm not mistake, it's supported since php 5.3.0

To check if you have installed you could create a php file with the following code <?php phpinfo(); ?> and find for references to PDFlib.

You could have a look in the below link: PDF

Edit:

As much as I know you can't create a pdf file without librarys / with core php.

Hope it helps you.

Upvotes: 1

Related Questions