Magento PDF发票内容布局问题

时间:2013-02-22 16:05:26

标签: magento

我根本不擅长PDF。我在下面添加了两个被称为“发票评论和订单评论”的评论。我遇到的麻烦是我不能将这两个部分放在一个好的单元格中。当我尝试$pdf->Cell之类的东西时,我只是得到了可怕的白色屏幕。出于某种原因,似乎只有drawText可以用于渲染文本。此外,评论还延伸到PDF页面。我试图输入换行符但没有成功。我猜它的b / c评论是数组。任何帮助都会很棒。

class Inchoo_Invoice_Model_Order_Pdf_Invoice extends Mage_Sales_Model_Order_Pdf_Invoice
{
    protected function insertImage($image, $x1, $y1, $x2, $y2, $width, $height, &$page)
    {
        if (!is_null($image)) {
            try{
                $width = (int) $width;
                $height = (int) $height;

                //Get product image and resize it
                $imagePath = Mage::helper('catalog/image')->init($image, 'image')
                    ->keepAspectRatio(true)
                    ->keepFrame(false)
                    ->resize($width, $height)
                    ->__toString();

                $imageLocation = substr($imagePath,strlen(Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB)));
                $image = Zend_Pdf_Image::imageWithPath($imageLocation);
                //Draw image to PDF
                $page->drawImage($image, $x1, $y1, $x2, $y2);
            }
            catch (Exception $e) {
                return false;
            }
        }
    }



    public function getPdf($invoices = array())
    {
        $width = 1000;
        $height = 1000;
        $this->_beforeGetPdf();
        $this->_initRenderer('invoice');

        $pdf = new Zend_Pdf();
        $this->_setPdf($pdf);
        $style = new Zend_Pdf_Style();
        $this->_setFontBold($style, 10);



        foreach ($invoices as $invoice) {
            if ($invoice->getStoreId()) {
                Mage::app()->getLocale()->emulate($invoice->getStoreId());
            }
            $page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
            $pdf->pages[] = $page;

            $order = $invoice->getOrder();

            /* Add image */
            $this->insertLogo($page, $invoice->getStore());


            /* Add address */
            $this->insertAddress($page, $invoice->getStore());

            /* Add head */
                        $page->drawText(Mage::helper('sales')->__('Invoice # ') . $invoice->getIncrementId(), 487, 780, 'UTF-8');
            $this->insertOrder($page, $order, Mage::getStoreConfigFlag(self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID, $order->getStoreId()));

            $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
            $this->_setFontRegular($page);


            /* Add table */
            $page->setFillColor(new Zend_Pdf_Color_RGB(0.93, 0.92, 0.92));
            $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
            $page->setLineWidth(0.5);

            $page->drawRectangle(25, $this->y, 570, $this->y -15);
            $this->y -=10;

            /* Add table head */
            $page->setFillColor(new Zend_Pdf_Color_RGB(0.4, 0.4, 0.4));
            $page->drawText(Mage::helper('sales')->__('Products'), 35, $this->y, 'UTF-8');
            //Added for product image
            $page->drawText(Mage::helper('sales')->__('Product Image'), 279, $this->y, 'UTF-8');
            $page->drawText(Mage::helper('sales')->__('SKU'), 125, $this->y, 'UTF-8');
            $page->drawText(Mage::helper('sales')->__('Price'), 380, $this->y, 'UTF-8');
            $page->drawText(Mage::helper('sales')->__('Qty'), 430, $this->y, 'UTF-8');
            $page->drawText(Mage::helper('sales')->__('Tax'), 480, $this->y, 'UTF-8');
            $page->drawText(Mage::helper('sales')->__('Subtotal'), 535, $this->y, 'UTF-8');

            $this->y -=15;

            $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));

            /* Add body */
            foreach ($invoice->getAllItems() as $item){
                if ($item->getOrderItem()->getParentItem()) {
                    continue;
                }

                if ($this->y < 15) {
                    $page = $this->newPage(array('table_header' => true));
                }

                /* Draw item */
                $page = $this->_drawItem($item, $page, $order);

                /* Draw product image */
                $productId = $item->getOrderItem()->getProductId();
                $image = Mage::getModel('catalog/product')->load($productId);
                $this->insertImage($image, 345, (int)($this->y + 0), 275, (int)($this->y+65), $width, $height, $page);
            }

            /* Add totals */
            $page = $this->insertTotals($page, $invoice);








/*************************** This Is The Invoice Comments ***********************************/
$_tempY = $this->y;
$this->y += 10;

$commentsCollection = $invoice->getCommentsCollection(true);

$internalcomments = "Internal Invoice Comments";    
$page->drawText($internalcomments, 35, $this->y, 'UTF-8'); 

            foreach($commentsCollection as $comm)
            {
                $page->drawText($comm->getData('comment'), 235, $this->y, 'UTF-8');
                $this->y -= 10;

            } 

/*************************** End Invoice Comments ***********************************/







            if ($invoice->getStoreId()) {
                Mage::app()->getLocale()->revert();
            }
        }





/************************* This Is The Order Comments *******************************/

$_tempY = $this->y;
$this->y -= 80;

$statusHistoryCollection = $order->getStatusHistoryCollection();
$customersection = "Order Customer Comments";
   $page->drawText($customersection, 35, $this->y, 'UTF-8'); 
foreach ($statusHistoryCollection as $statushistory) {  
   $page->drawText($statushistory->getComment(), 35, $this->y, 'UTF-8');
   $this->y -= 10;
}

$this->y = $_tempY;

/************************* End Order Comments *******************************/



        $this->_afterGetPdf();

        return $pdf;
    }



}

1 个答案:

答案 0 :(得分:1)

我已经为您完成了发票评论部分,因此您可以将相同的技术应用于订单评论。我在沙盒上测试了这个并且它有效。您可以将120行中$textChunk = wordwrap($comm->getData('comment'), 120, "\n");的值更改为适用于您的发票的最佳值。我在Internal Invoice Comments添加了一个表标题,但如果需要,可以将其删除。

/*************************** This Is The Invoice Comments ***********************************/

    $this->_setFontRegular($page, 10);

    // Begin table header
    $page->setFillColor(new Zend_Pdf_Color_RGB(0.93, 0.92, 0.92));
    $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
    $page->setLineWidth(0.5);
    $page->drawRectangle(25, $this->y, 570, $this->y -15);
    $this->y -= 10;
    $page->setFillColor(new Zend_Pdf_Color_RGB(0, 0, 0));
    // end table header


    $_tempY = $this->y;

    $commentsCollection = $invoice->getCommentsCollection(true);

    $internalcomments = "Internal Invoice Comments";
    $page->drawText($internalcomments, 35, $this->y, 'UTF-8');

    $this->y -= 15;

    foreach($commentsCollection as $comm)
    {
            $textChunk = wordwrap($comm->getData('comment'), 120, "\n");
            foreach(explode("\n", $textChunk) as $textLine){
                    if ($textLine!=='') {
                            $page->drawText(strip_tags(ltrim($textLine)), 35, $this->y, 'UTF-8');
                            $this->y -= 15;
                    }
            }

     }

/*************************** End Invoice Comments ***********************************/

P.S。 Magento PDF例程中的部分新行改编自PHP + PDF Line Break。谢谢@shaune!