Feature Request: Add Order Comments to Generated (Print) PDFs Order & Packing Slip
Hi,
Thanks for adding the API features of this module - Definitely helped with this business case. However, I encountered an issue. Most businesses, don't have the Magento Admin panel open when processing orders and as such, they print out the Order, or Packing Slip PDF and give this to their delivery teams.
It would be nice to add these comments optionally onto the Order / Packing Slip PDF's
Let me know if this is a feature you're interested in completing. It would make this module fully usable for all cases, from integrations to companies that print off PDFs directly from Magento.
Thanks again for the module, it's great!
Robert
hi @duffner , If you still want this functionality, I can look into this when i have some free time somewhere either this month or the next.
I didnt find the print order PDF button in Magento, but I did find the print packing slip. Was it just the packing slip that needed this addition?
@boldsidney, here is the business case: Many of my existing customers use this module to place "Shipping Comments" for their order, while this works great, we use the "Print Invoices" feature from the mass action drop down to give to our shipping department to process orders. Having the notes on the printed documented would prevent our team from having to review every order in the Admin panel. While the "Print Invoices" is our use case, I think many would make use of this in the "Print Packing Slips" PDF as well. As this feature will likely be specific per Magento installation, offering this as an admin configuration would be ideal.
Was there any update on this? I'm also interested in seeing the order comments in the pdf invoice printout. Common use case is that invoices will be printed in batch, and will be processed using the printouts
I've created a patch based on https://github.com/mathijsm/magento2-ordercomments/commit/5dde194419f49e20205e73b7cb957bf0e9674488 . This way, the core files are used.
/patches/print-bold-order-comment-on-invoice-and-shipment-pdf.patch
--- module-sales/Model/Order/Pdf/AbstractPdf.php
+++ module-sales/Model/Order/Pdf/AbstractPdf.php
@@ -1124,4 +1124,78 @@
}
return $top;
}
+
+ // START add boldcommerce/magento2-ordercomments to PDF's
+ protected function insertComment(\Zend_Pdf_Page $page, $comment)
+ {
+ if (!$comment) {
+ return $page;
+ }
+
+ if ($this->y < 50) {
+ $page = $this->newPage();
+ }
+
+ $this->drawCommentHeader($page);
+ $comment = $this->formatComment($comment);
+ $page = $this->drawCommentBody($page, $comment);
+
+ return $page;
+ }
+
+ protected function drawCommentHeader(\Zend_Pdf_Page $page)
+ {
+ $this->_setFontRegular($page, 10);
+ $page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
+ $page->setLineWidth(0.5);
+ $page->drawRectangle(25, $this->y, 570, $this->y - 15);
+ $page->setFillColor(new \Zend_Pdf_Color_Rgb(0, 0, 0));
+ $page->drawText(__('Order Comment'), 35, $this->y -10);
+ $this->y -= 30;
+ return $page;
+ }
+
+ protected function drawCommentBody(\Zend_Pdf_Page $page, $commentLines)
+ {
+ $this->prepareCommentBodyBlock($page, count($commentLines));
+
+ foreach($commentLines as $index => $line) {
+ $page->drawText($line, 35, $this->y);
+ $this->y -= 10;
+
+ if ($this->y <= 15) {
+ $page = $this->newPage();
+ $remainingLines = count($commentLines) - ($index + 1);
+ $this->drawCommentHeader($page);
+ $this->prepareCommentBodyBlock($page, $remainingLines);
+ }
+ }
+ return $page;
+ }
+
+ protected function prepareCommentBodyBlock(\Zend_Pdf_Page $page, $numLines)
+ {
+ $page->setFillColor(new \Zend_Pdf_Color_GrayScale(1));
+ $page->drawRectangle(
+ 25,
+ $this->y + 15,
+ 570,
+ max($this->y - (10 * $numLines) + 10, 15)
+ );
+
+ $page->setFillColor(new \Zend_Pdf_Color_Rgb(0, 0, 0));
+ }
+
+ protected function formatComment($text)
+ {
+ $result = [];
+ foreach (explode("\n", $text) as $str) {
+ foreach ($this->string->split($str, 130, true, true) as $part) {
+ $result[] = $part;
+ }
+ $result[] = '';
+ }
+ return $result;
+ }
+ // END add boldcommerce/magento2-ordercomments to PDF's
}
--- module-sales/Model/Order/Pdf/Invoice.php
+++ module-sales/Model/Order/Pdf/Invoice.php
@@ -148,6 +148,14 @@
);
/* Add document text and number */
$this->insertDocumentNumber($page, __('Invoice # ') . $invoice->getIncrementId());
+
+ // START add boldcommerce/magento2-ordercomments to PDF's
+ $page = $this->insertComment(
+ $page,
+ $order->getBoldOrderComment()
+ );
+ // END add boldcommerce/magento2-ordercomments to PDF's
+
/* Add table */
$this->_drawHeader($page);
/* Add body */
--- module-sales/Model/Order/Pdf/Shipment.php
+++ module-sales/Model/Order/Pdf/Shipment.php
@@ -139,6 +139,14 @@
);
/* Add document text and number */
$this->insertDocumentNumber($page, __('Packing Slip # ') . $shipment->getIncrementId());
+
+ // START add boldcommerce/magento2-ordercomments to PDF's
+ $page = $this->insertComment(
+ $page,
+ $order->getBoldOrderComment()
+ );
+ // END add boldcommerce/magento2-ordercomments to PDF's
+
/* Add table */
$this->_drawHeader($page);
/* Add body */
To be used with https://github.com/cweagans/composer-patches
composer.json
"extra": {
"patches": {
"magento/module-sales": {
"boldordercomments": "./patches/print-bold-order-comment-on-invoice-and-shipment-pdf.patch"
}
}
}
No way @evs-xsarus just solved this 2 year open case just when I stumbled upon it. Legend and perfect timing!
I don't use this module anymore, but it's better practice to override these files in your own module. Editing core files is a bad idea.
Create a custom module and include in /etc/admindhtml/di.xml lines like <preference for="Magento\Sales\Model\Order\Pdf\Shipment" type="VENDOR\MODULE\Model\Order\Pdf\Shipment" />
And put your new php files in the custom module.
@twoatechguy well, I agree partially. Doing it with a patch, gives me a warning should the core file change. This is a trigger for me to check if the functionality must be changed.
A preference on this PDF class also means overriding a long function. Should the function change a bit in the core, I'll never know.
Basically, when I can use a before or after plugin, I'll use a local module, otherwise, I'll patch.
@evs-xsarus agreed, but you will be called out by 3rd party auditors who don't understand the reasoning.