Creating Custom Print Formats in PHP for Merciglobal Cloud ERP¶
Developers working with Merciglobal Cloud ERP can use PHP to create custom print formats. These formats allow printing and emailing of documents like invoices, order forms, and more, with company branding and detailed formatting.
Naming Convention¶
- Your PHP file should follow this naming pattern:
Invoice_Printing_{tablename}.php
{tablename}
will be dynamically replaced with the relevant table name.
- File should essentially be placed in the /system/ folder.
Core Workflow¶
Here is an overview of how the print format PHP script functions:
1. Initialization¶
- Start a PHP session.
- Include necessary functions using
require "funcs.php"
. - Set working directory using
chdir('..')
. - Use
merciPDF()
to create a PDF instance.
2. Fetch Data¶
- Get runtime parameters such as
mode
(PRINT/EMAIL) andid
. - Query the database for company and transaction details.
- Format the address and contact details for print.
3. PDF Generation¶
- Set PDF styles (font, size, bold, color).
- Add elements to PDF:
- Company name, address, and logo
- Customer details
- Software and pricing info
- Terms & conditions
4. Final Actions¶
- If
mode = PRINT
, call$pdf->end()
and mark the transaction as printed. - If
mode = EMAIL
, generate the PDF file and send it usingdoDocEmail()
.
Key PDF Methods Used¶
Method | Purpose |
---|---|
say(x, y, text) |
Print text at (x, y) position |
sayImage(url, x, y, width, height) |
Insert image |
box(x, y, height, width) |
Draw a box |
skip(n) |
Skip n lines |
line(row, x, height, width) |
Draw horizontal line |
saveas(filepath) |
Save PDF to server |
end() |
Finalize and output PDF |
Use Cases¶
This PHP-based print format is ideal for: - Customer invoices - Software order forms - Delivery challans - Quotation approvals
Email Functionality¶
The doDocEmail()
function takes care of sending the document with a subject like Order Confirmation... along with the attached PDF. Ensure correct email addresses are fetched and validated.
Sample Code¶
Below is a full sample PHP code snippet that can be copied and customized to generate a print format from the trn_orderform
table. All code comments are preserved to aid developers with understanding and customization.
<?php
// Start PHP session and change working directory
session_start();
chdir('..');
require "funcs.php";
// Fetch mode (PRINT or EMAIL)
$mode = $_GET['mode'];
$pdf = merciPDF();
// Fetch master ID and company ID
$masterid = $_GET['id'];
$printcond = "a.id = $masterid";
$cid = GetCompanyID();
// Fetch company details
$cq_comp="SELECT * FROM company a WHERE a.id=".$cid;
$row_comp = getrow(myExecute($cq_comp));
// Format company data
$company = $row_comp['company'];
$comp_addr1 = proper(lastcomma($row_comp['street1'])) . proper($row_comp['street2']);
$comp_addr2 = proper(lastcomma($row_comp['street3'])) . proper($row_comp['street4']) . ' ' . proper(lastcomma($row_comp['city']));
$comp_website ="Web:mercicloud.com," . strtolower($row_comp['email']);
$comp_mobile = ($row_comp['mobile1']!="" || $row_comp['phone1']!="") ? "Mob. " .$row_comp['mobile1']." Ph. ".$row_comp['phone1'] : '';
// Fetch transaction details
$cq="SELECT * FROM trn_orderform a WHERE $printcond";
$res = myExecute($cq);
$row = getrow($res);
$email = $row['email'];
// Begin PDF formatting
$pdf->skip(3);
$pdf->setFontEx('Arial', '', 12);
$pdf->setsize(18);
$pdf->setBold(true);
$pdf->setColorEx('#808080');
$pdf->say(0, 8, $company);
$pdf->setColorEx();
$pdf->setFontEx('Arial', '', 10);
$pdf->say(0, 51, $comp_addr1);
$pdf->sayImage('https://mercicloud.com/cipl/merci150.jpg', $pdf->row, 37, 3, 12);
$pdf->skip(1);
$pdf->say(0, 51, $comp_addr2);
$pdf->say(0, 51, $comp_website);
$pdf->line($pdf->row, 8, 0.1, 70);
$pdf->skip(3);
$pdf->say(0, 0, "SOFTWARE ORDER", 'C');
$pdf->skip(3);
$pdf->box($pdf->row-1, 8, 14, 70);
// Print basic details
$pdf->say(0, 50, "Date");
$pdf->say(0, 59, dtoc($row['orderdate']));
$pdf->say(0, 8, "Company");
$pdf->say(0, 17, strtoupper($row['company']));
$pdf->say(0, 8, "C.Person");
$pdf->say(0, 17, proper($row['cperson']));
$pdf->say(0, 8, "Sign.Auth");
$pdf->say(0, 17, proper($row['signauthority']));
// Print address with line breaks
$address = explode((strpos($row['address'], chr(13)) !== false) ? chr(13) : ',', $row['address']);
foreach ($address as $i => $line) {
$pdf->skip(1);
if ($i == 0) $pdf->say(0, 8, 'Address');
$pdf->say(0, 17, trim($line));
}
// Additional customer information
$pdf->say(0, 8, 'City'); $pdf->say(0, 17, $row['surat']);
$pdf->say(0, 50, 'Mobile'); $pdf->say(0, 59, $row['mobile']);
$pdf->say(0, 8, 'Email'); $pdf->say(0, 17, $row['email']);
$pdf->say(0, 8, 'GST No'); $pdf->say(0, 17, $row['gstin']);
$pdf->say(0, 50, 'Business'); $pdf->say(0, 59, $row['business']);
// Software list
$software = explode('*', $row['software']);
foreach ($software as $i => $item) {
$pdf->skip(1);
if ($i == 0) $pdf->say(0, 8, 'Software:');
$pdf->say(0, 17, trim($item));
}
// Charges and remarks
$pdf->say(0, 8, 'S/w Charges'); $pdf->say(0, 22, $row['swcharge']);
$pdf->say(0, 50, 'Trf Masters'); $pdf->say(0, 59, $row['trf_masters']);
$pdf->say(0, 8, 'Server Charges'); $pdf->say(0, 22, $row['serverchg']);
$pdf->say(0, 50, 'Trf Data'); $pdf->say(0, 59, $row['trf_data']);
$pdf->say(0, 8, 'Cal.Licenses'); $pdf->say(0, 22, $row['cal_license']);
$pdf->say(0, 8, 'Sales Person'); $pdf->say(0, 22, $row['salesperson']);
$pdf->say(0, 8, 'Remarks'); $pdf->say(0, 22, proper($row['remarks']));
// Terms and conditions
$pdf->say(0, 8, 'Terms:');
$pdf->say(0, 8, '1. GST extra at applicable rates.');
$pdf->say(0, 8, "2. Nidhi Infocomm will finalize s/w issues with aforesaid only auth.sign person of the customer.");
$pdf->say(0, 8, '3. All terms specified in the accompanying email supersede the terms on this order form.');
// Output the PDF or send via email
if ($mode == 'PRINT') {
$pdf->end();
printed('trn_orderform', $masterid);
} else {
$pdffile = HomeFolder() . "_order_" . $row['id'] . ".pdf";
$pdf->saveas($pdffile);
if ($email !== '') {
foreach (explode(',', strtolower($email)) as $emailto) {
echo "Sending email to...$emailto<br>";
echo doDocEmail($emailto, '', 'Order Confirmation...', '', $pdffile, 'trn_orderform', $masterid) . '<br>';
}
} else {
echo "<h2>No Email Address Specified In Quotation Information</h2>";
}
}
?>
For further customization and automation within Merciglobal Cloud ERP, contact the development team or explore more developer documentation.
Keep your documents elegant, professional, and automated with PHP-powered print formats.