For those who want to export records to pdf

61 views
Skip to first unread message

Nathan B.

unread,
Feb 26, 2018, 6:50:32 PM2/26/18
to Xataface
First of all, thank you Steve for this great app.

Then, I saw few people on this forum who wanted to export record directly to pdf format.
Here is my solution (probably not the best solution, but do the job...).

1) I use TCPDF library (https://tcpdf.org/) : download it from github, and export in myapp/modules/tcpdf.

2) Then, I created exporterpdf.php in myapp/actions directory, like that (change it with your own values) :

<?php
class actions_exporterpdf {
   
function handle(&$params){
        require_once
(__DIR__.'/../modules/TCPDF/tcpdf.php');

       
// Get my record values
        $app
=& Dataface_Application::getInstance();
        $rec
= $app->getRecord();
        $id
= $rec->val('id');
        $name
= $rec->val('name');

       
// create new PDF document
        $pdf
= new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

       
// set document information
        $pdf
->SetCreator(PDF_CREATOR);
        $pdf
->SetAuthor('MY_NAME');
        $pdf
->SetTitle('MY_TITLE');
        $pdf
->SetSubject('MY_SUBJECT');
        $pdf
->SetKeywords('MY_KEYWORDS');

       
// set default header data
       
// $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 065', PDF_HEADER_STRING);

       
// set header and footer fonts
        $pdf
->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $pdf
->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

       
// set default monospaced font
        $pdf
->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

       
// set margins
        $pdf
->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf
->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf
->SetFooterMargin(PDF_MARGIN_FOOTER);

       
// set auto page breaks
        $pdf
->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

       
// set image scale factor
        $pdf
->setImageScale(PDF_IMAGE_SCALE_RATIO);

       
// set some language-dependent strings (optional)
       
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
            require_once
(dirname(__FILE__).'/lang/eng.php');
            $pdf
->setLanguageArray($l);
       
}


// ---------------------------------------------------------

// set font
$pdf
->SetFont('helvetica', '', 13);

// add a page
$pdf
->AddPage();

// set some text to print
$html
= <<<EOD
<h1 style="text-align: center;color:red;">MY RECORD</h1>
<span>[$id] $name</
span>

EOD
;

// Print text using writeHTMLCell()
$pdf
->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);

// ---------------------------------------------------------

//Close and output PDF document
$pdf
->Output('output.pdf', 'I');


   
}
}
?>


3) In the actions.ini of your table directory, add something like to get icon on detail view :

[exporterpdf]
    category
= record_tabs
    url
= "{$this->url('-action=exporterpdf')}"
    target
="_blank"
    order
=-10
    icon
= "MY_ICON.png"
    description
= Export to PDF
    label
= ""

And it's done, you can export your record to PDF format.

Nathan

Steve Hannah

unread,
Feb 27, 2018, 4:53:40 PM2/27/18
to xata...@googlegroups.com
Thanks.  This will surely help a lot of people out as it is a frequent request (exporting to PDF).  Not sure if you ran across this in your travels, but another approach is to use the FDF module.

Best regards

Steve

--
You received this message because you are subscribed to the Google Groups "Xataface" group.
Visit this group at https://groups.google.com/group/xataface.
To view this discussion on the web visit https://groups.google.com/d/msgid/xataface/da8e0432-bce8-4399-b595-3465a66d3e25%40googlegroups.com.



--
Steve Hannah
Web Lite Solutions Corp.
Reply all
Reply to author
Forward
0 new messages