How do i create a printed pdf from fields?

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
User avatar
urichard
Veteran Member
Posts: 87
Joined: 2018-11-01 12:11

How do i create a printed pdf from fields?

Post by urichard » 2022-12-20 10:45

Hi

Ho do I print to a pdf all the fields in a table and also the company details with logo etc, I have appgini javascript library so can I create a button whereby I can then print each invoice out?
[email protected][/color]

Kind Regards
Richard

User avatar
urichard
Veteran Member
Posts: 87
Joined: 2018-11-01 12:11

Re: How do i create a printed pdf from fields?

Post by urichard » 2022-12-22 13:10

Helo?
[email protected][/color]

Kind Regards
Richard

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How do i create a printed pdf from fields?

Post by jsetzer » 2022-12-22 14:49

As a starting point, have a look at Mozilla's library which allows coding PDFs:
https://mozilla.github.io/pdf.js/

Or consider purchasing a commercial library like PhpDocx Advanced:
https://www.phpdocx.com

Or search for reporting Tools and try to integrate with your database.

Or search for html2pdf, create HTML pages and convert them to PDF.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
urichard
Veteran Member
Posts: 87
Joined: 2018-11-01 12:11

Re: How do i create a printed pdf from fields?

Post by urichard » 2022-12-22 15:37

Oky thanks Jsetzer.

IS there any hope of Appgini bringing out an easy to follow install plugin to the appgini family?

I for one do not know alot about it but slowly learning, i would still prefer something i can easily install and setup like the Appgini Helper Javascript Library and functions
[email protected][/color]

Kind Regards
Richard

User avatar
urichard
Veteran Member
Posts: 87
Joined: 2018-11-01 12:11

Re: How do i create a printed pdf from fields?

Post by urichard » 2022-12-22 16:00

For an example using the northwind demo:
I can click on print preview and click on print but i would like to add my logo and company details ontop of the printout and also include custom content at the footer when i click print preview, can i do this now within appgini?

Meaby to print the calculation box is a bit far fetched i can understand but its my plan to print it as in screemshot

Please see screenshots
Attachments
Screenshot_3.jpg
Screenshot_3.jpg (88.85 KiB) Viewed 1577 times
Screenshot_5.jpg
Screenshot_5.jpg (87.43 KiB) Viewed 1577 times
Screenshot_6.jpg
Screenshot_6.jpg (60.06 KiB) Viewed 1577 times
Screenshot_7.jpg
Screenshot_7.jpg (64.85 KiB) Viewed 1577 times
[email protected][/color]

Kind Regards
Richard

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How do i create a printed pdf from fields?

Post by jsetzer » 2022-12-22 16:09

Would be great to have a cheap, professional, version-compatible, maintained, easy to use product.

I can estimate how much time it would take to develop a Designer tool and a PDF renderer in PHP and Javascript. At the AppGini price level of 49$ per plugin I cannot see that any professional company can build, deploy and maintain such a solution - at least not at the cost index we have here in Germany. Development here in Germany would probably cost several thousand Euro.

Cheapest professional tool I have seen, yet, is PhpDocx Advanced library (commercial*) starting at 599$. You can design a Microsoft Word (commercial*) DOCX with images, text-blocks, paragraphs, placeholders etc.. That library allows you to replace the placeholders, generate a filled DOCX, then convert it into PDF, all by using their library functions in PHP code. If you want PDFs, it is important to purchase the Advanced license, at least. The cheaper standard version can not do this.

* I am not related to those two companies


If anyone else is using different tools, it would be great if you could post your experiences.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How do i create a printed pdf from fields?

Post by jsetzer » 2022-12-22 16:19

You can also customize the HTML output by modifying the templates or create a completely new custom page in HTML, then use the 'Print to PDF' printer driver in your browser's print dialog.

But caution: if you need 100% accurate printouts, this will be much work coding in HTML, if possible at all. Printing HTML is browser- and browser-settings-dependent. The printout may look different on different machines due to local (!) browser settings like margins, headlines, page numbering, scale, page orientation etc.. as mentioned before: if you need 100% accurate printouts, you will need something like PDF creation.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
zibrahim
Veteran Member
Posts: 137
Joined: 2020-01-28 18:30
Location: Malaysia

Re: How do i create a printed pdf from fields?

Post by zibrahim » 2022-12-23 02:43

Hi Richard,
You may want to consider TCPDF (a free and open source software PHP class for generating PDF documents).
https://tcpdf.org
I managed to generate invoices, work orders etc by using this PHP library.

Have a nice day.
Zala.
Appgini 24.10.1579, MacOS 14.3.1 Windows 11 on Parallels.

User avatar
urichard
Veteran Member
Posts: 87
Joined: 2018-11-01 12:11

Re: How do i create a printed pdf from fields?

Post by urichard » 2023-01-01 16:45

Thanks Zibrahim

Would you mind to share with me the steps you took? I would really appreciate it

i had a look at it and it seems pretty complicated?
[email protected][/color]

Kind Regards
Richard

User avatar
zibrahim
Veteran Member
Posts: 137
Joined: 2020-01-28 18:30
Location: Malaysia

Re: How do i create a printed pdf from fields?

Post by zibrahim » 2023-01-02 01:09

Hi Richard,
Happy to share it with you and others. In this example, I am using Purchase Order as sample.

1.Download and Save the TCPDF library in the main folder (I renamed it to pdf_library)
https://github.com/tecnickcom/tcpdf
SCR-20221223-bci.png
SCR-20221223-bci.png (16.47 KiB) Viewed 1475 times
2. Create a PHP file in the main folder (example purchase_order_pdf.php) (not in the hooks folder) with the following codes

Code: Select all

<?php
date_default_timezone_set('Asia/Kuala_Lumpur');

// standard Appgini file headings
$currDir = dirname(__FILE__);
include "$currDir/defaultLang.php";
include "$currDir/language.php";
include "$currDir/lib.php";

// specify the location of the pdf library folder
require 'pdf_library/tcpdf.php';

// grant access to all users who have access to the purchase_order table
$purchase_order_from = get_sql_from('purchase_order');
if (!$purchase_order_from) {
    exit(error_message('Access denied!', false));
}

// check if purchase_order_id valid or not
$purchase_order_id = intval($_REQUEST['id']);
if (!$purchase_order_id) {
    exit(error_message('Invalid Purchase Order ID!', false));
}


// START DEFINING PURCHASE ORDER SECTIONS
class myPDF extends TCPDF {
    // START OF PURCHASE ORDER TOP SECTION >>>>>
    public function purchase_order_top() {
        // set color for background
        $this->SetFillColor(224, 224, 224);

        // retrieve purchase_order id
        $purchase_order_id = intval($_REQUEST['id']);

        // retrieve purchase_order info
        $res = sql("SELECT `purchase_order`.*, `supplier`.* FROM `purchase_order` LEFT JOIN `supplier` ON `purchase_order`.`supplier_id` = `supplier`.`id` WHERE `purchase_order`.`id` = {$purchase_order_id}", $eo);
        $purchase_order = db_fetch_assoc($res);

        $this->Image('images/logo.png', 10, 10, 48, 0, '', '', '', true, 300, 'L');

        $this->SetFont('', 'B', 10);
        $this->Cell(180, 0, 'Your Company Name', 0, 0, 'R');
        $this->Ln();

        $this->SetFont('', '', 8);
        $this->Cell(180, 0, 'Address Line 1', 0, 0, 'R');
        $this->Ln();

        $this->Cell(180, 0, 'Address Line 2', 0, 0, 'R');
        $this->Ln();

        $this->Cell(120, 0, '', 0, 0, 'L');
        $this->Cell(60, 0, 'Postcode, City', 0, 0, 'R');
        $this->Ln();

        $this->Cell(120, 0, '', 0, 0, 'L');
        $this->Cell(60, 0, 'State, Country', 0, 0, 'R');
        $this->Ln();


        $this->SetFont('', 'I', 9);
        $this->Cell(120, 0, 'Supplier or Customer Info', 0, 0, 'L');
        $this->Ln(8);

        $this->SetFont('', 'B', 9);
        $this->Cell(120, 0, $purchase_order['supplier_name'], 0, 0, 'L');
        $this->SetFont('', 'B', 12);
        $this->Cell(60, 0, ' PURCHASE ORDER ', 1, 0, 'J', 1);
        $this->Ln(7);

        $this->SetFont('', '', 8);
        $this->Cell(120, 0, $purchase_order['address_1'], 0, 0, 'L');
        $this->Cell(40, 0, 'Purchase Order No : ', 0, 0, 'R');
        $this->SetFont('', 'B');
        $this->Cell(20, 0, $purchase_order_id, 0, 0, 'R');
        $this->Ln();

        $this->SetFont('', '', 8);
        $this->Cell(120, 0, $purchase_order['address_2'], 0, 0, 'L');
        $this->Cell(40, 0, 'Date : ', 0, 0, 'R');
        $this->Cell(20, 0, date('d-m-Y', strtotime($purchase_order['date'])), 0, 0, 'R');
        $this->Ln();

        $this->Cell(120, 0, $purchase_order['postcode'] . ', ' . $purchase_order['city'], 0, 0, 'L');
        $this->Cell(40, 0, '', 0, 0, 'R');
        $this->Cell(20, 0, '', 0, 0, 'R');
        $this->Ln();

        $this->Cell(120, 0, $purchase_order['state'] . ', ' . $purchase_order['country'], 0, 0, 'L');
        $this->Cell(40, 0, '', 0, 0, 'R');
        $this->Cell(20, 0, '', 0, 0, 'R');
        $this->Ln(9);
    }
    // END OF PURCHASE ORDER TOP SECTION <<<<<<<

    // START OF PURCHASE ORDER ITEMS SECTION >>>>>>>
    public function purchase_orderItems() {

        // create some HTML content
        $html = '
        <table border="1" cellspacing="0" cellpadding="4">
            <tr>
                <th style="width:30px" align="center">#</th>
                <th style="width:270px" align="left">Description</th>
                <th style="width:60px" align="right">Unit Price</th>
                <th style="width:50px" align="right">Quantity</th>
                <th style="width:30px" align="right">UOM</th>
                <th style="width:70px" align="right">Line Total</th>
            </tr>
        ';

        // retrieve purchase_order_id 
        $purchase_order_id = intval($_REQUEST['id']);

        // retrieve purchase_order_item info
        $counter = 0;
        $sub_total = 0;
        $item = array();
        $res = sql("SELECT *, `purchase_order_item`.`product_description` AS `description_1` FROM `purchase_order_item` LEFT JOIN `product` ON `purchase_order_item`.`product_id` = `product`.`id` WHERE `purchase_order_id` = {$purchase_order_id} ORDER BY `position` ASC", $eo);
        while ($row = db_fetch_assoc($res)) {
            $item[] = $row;
            $html .= '
            <tr>
                <td style="width:30px" align="center">' . ($counter + 1) . '</td>
                <td style="width:270px" align="left">' . '<strong>' . $item[$counter]['product_name'] . '</strong><br>' . nl2br($item[$counter]['description_1']) . '</td>
                <td style="width:60px" align="right">' . $item[$counter]['unit_price'] . '</td>
                <td style="width:50px" align="right">' . $item[$counter]['quantity'] . '</td>
                <td style="width:30px" align="right">' . $item[$counter]['uom'] . '</td>
                <td style="width:70px" align="right">' . $item[$counter]['line_total'] . '</td>
            </tr>
            ';
            $counter++;
            $sub_total += $row['line_total'];
        }

        // retrieve purchase_order info
        $res = sql("SELECT * FROM `purchase_order` WHERE `purchase_order`.`id` = {$purchase_order_id}", $eo);
        $purchase_order = db_fetch_assoc($res);

        // purchase_order subtotal, tax and total
        $html .= '

            <tr>
                <td colspan="5" align="right" style="font-weight:bold">SUBTOTAL</td>
                <td style="width:70px" align="right" style="font-weight:bold">' . number_format($sub_total, 2, '.', '') . '</td>
            </tr>
            <tr>
                <td colspan="5" align="right" style="font-weight:bold">TAX</td>
                <td style="width:70px" align="right" style="font-weight:bold">' . number_format($sub_total * $purchase_order['tax'] / 100, 2, '.', '') . '</td>
            </tr>
            <tr>
                <td colspan="5" align="right" style="font-weight:bold" > (MYR) TOTAL</td>
                <td style="width:70px" align="right" style="font-weight:bold">' . number_format($sub_total + ($sub_total * $purchase_order['tax'] / 100), 2, '.', '') . '</td>
            </tr>

        </table>

        <style>
        table {
            border-collapse:collapse;
        }
        th,td {
            border:1px solid #CCCCCC;
        }
        table tr th {
            border:1px solid #888;
            background-color:#CCCCCC;
            color:#000;
            font-weight:bold;
        }
        </style>

        ';

        // output the items HTML content
        $this->writeHTML($html, true, false, false, false, '');

        // print purchase_order remark if have data
        if (($purchase_order['purchase_order_message'])) {
            $html = '
            <p><strong>Remarks :</strong></p>
            <p>' . nl2br($purchase_order['purchase_order_message']) . '</p>
            ';
            // output the HTML content
            $this->writeHTML($html, true, false, false, false, '');
        }

        $this->Ln(15);

        // ending caption
        $this->Cell(180, 5, '------------- Thank you and have a nice day -------------', 0, 0, 'C');
    }

    // END OF PURCHASE ORDER ITEMS SECTION <<<<<<<

    // START OF FOOTER SECTION >>>>>>>
    public function Footer() {
        // print the page number
        $this->SetY(-15);
        $this->Cell(0, 10, 'Page ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 0, 0, 'C');
    }
    // END OF FOOTER SECTION <<<<<<<

}

// GENERATING THE PDF SECTION
$pdf = new myPDF();

// remove default Header and Footer
$pdf->setPrintHeader(false);

// 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 - 10, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

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

// set the page (paper) layout and size
$pdf->AddPage('P', 'A4', '0');

// get the content (calling functions)
$pdf->purchase_order_top();
$pdf->purchase_orderItems();

// set the filename for saving
$filename = date("Ymd_His") . '_purchase_order_' . $purchase_order_id . '.pdf';

// output in specific folder & on screen
// $pdf->Output($_SERVER['DOCUMENT_ROOT'] . 'FOLDER_NAME/'. $filename, 'FI');

// output on screen
$pdf->Output($filename, 'I');
Notes : You may need to change the tablename and fieldnames according to your environment and also the format or layout of your pdf document.

3. Add the button to open the file (in my case, i put it in purchase order DV using Appgini Helper js library)
file: hooks/purchase_order-dv.js

Code: Select all

// create additional buttons and functions (PDF)
var idValue = $j('[name=SelectedID]').val();
if (idValue) {
    var actionbuttons = dv.actionbuttons;
    var group = actionbuttons.addGroup('Additional Functions');
    group.addButton(
        'PO PDF',
        function () {
            // get ID value of current record and url-encode it
            var id = encodeURIComponent($j('[name=SelectedID]').val());
            // open the url
            window.open('purchase_order_pdf.php?id=' + id, '_blank', 'width=1100, height=800');
            // alert("Clicked!");
        },
        Variation.Warning
    );
}
Note : you can also change the above codes to suit your requirement

Hope this will help you and others as well.
Cheers and Happy New Year.
Zala.
Appgini 24.10.1579, MacOS 14.3.1 Windows 11 on Parallels.

Post Reply