0

谁能指导我如何在magento中生成愿望清单项目(产品)的pdf?

4

1 回答 1

3

将此代码包装在您的自定义控制器中。

public function whishlistAction()
{    
    $session = Mage::getSingleton('customer/session', array('name'=>'frontend'));

    if(!$session->isLoggedIn()) 
    {
        $this->_redirect(Mage::getUrl('customer/account'));
    }  

    $customer = Mage::getModel('customer/customer')->load($session->getId());
    $wishList = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer);
    $wishListItemCollection = $wishList->getItemCollection();

    $pdf = new Zend_Pdf(); 

    $pageCount = 0;
    $collectionPreperedForPagesKey=0;
    $collectionPreperedForPages = array(); 

    foreach ($wishListItemCollection as $w)
    {
        $pageCount = $pageCount+1;
        if($pageCount==4)
        { 
            $collectionPreperedForPagesKey=$collectionPreperedForPagesKey+1;
            $pageCount=0;
        } 
        $collectionPreperedForPages[$collectionPreperedForPagesKey][]=$w;
    } 
    foreach ($collectionPreperedForPages as $c)
    {
        $pdf->pages[] = $this->createWhishlistPage($c); 
    }


    $pdfString = $pdf->render();
    header("Content-Disposition: attachment; filename=whishlist.pdf");
    header("Content-type: application/x-pdf");
    echo $pdfString;
    exit;
}

public function getImagePath($product)
{
    $imageUrl = Mage::getModel('catalog/product_media_config')
        ->getMediaUrl( $product->getImage() ); 

    $baseDir =  Mage::getBaseDir() ; 
      $withoutIndex = str_replace('index.php/','', Mage::getBaseUrl());

      $imageWithoutBase = str_replace($withoutIndex,'' , $imageUrl);
       $imagePath = $baseDir.DIRECTORY_SEPARATOR.$imageWithoutBase ;

       return $imagePath;
}

public function getImageSizesWithAspectRatio($widthImage,$heightImage,$widthBox,$heightBox)
{ 
    if (($widthBox >= $widthImage) && ($heightBox >= $heightImage)) 
    {
          return array('width'=>$widthImage,'height'=>$heightImage);
    }  

    $resizeRatio = 1;
    if($widthBox < $widthImage) 
    {
       $resizeRatio = $widthImage/$widthBox; 
       $widthImage = $widthBox;   
    }

     if($heightBox < $heightImage) 
     {
        if($resizeRatio!=1)
        {
            $heightImage = $heightImage/$resizeRatio;
        }

        if($heightImage>$heightBox)
        {
             $resizeRatio = $heightImage/$heightBox;
               $heightImage = $heightBox; 
               $widthImage = $widthImage/$resizeRatio; 
        }
     }   
     return array('width'=>$widthImage,'height'=>$heightImage);
}

public function createWhishlistPage($wishListItemCollection)
{ 
    $lightFont = Zend_Pdf_Font::fontWithPath($this->getFont('light'));
    $regularFont = Zend_Pdf_Font::fontWithPath($this->getFont('regular'));
    $boldFont = Zend_Pdf_Font::fontWithPath($this->getFont('bold')); 

    $page1 = new Oxidian_Pdf_Helper_Page(Zend_Pdf_Page::SIZE_A4);
    $page1->setFillColor(Zend_Pdf_Color_Html::color('#75645E'));

    $page1->setLineColor(Zend_Pdf_Color_Html::color('#75645E'));
    $page1->setLineWidth(1); 

    $headerFontSize = 40;
    $headerLeft = 20; 
    $headerTop = $page1->getHeight()-$headerFont-50;

    $page1->setFont($lightFont, $headerFontSize);

    $contentTopFreeSpace = $headerTop-70; 
    $contentLeft = 20;
    $contentRight = $page1->getWidth()-20;

    $page1->drawText('JORDAN', $headerLeft,$headerTop);
    $page1->setFont($regularFont, $headerFontSize/2);
    $page1->drawText('My dreamboard', $page1->getWidth()/2+40,$headerTop);

    $tableHeaderFont = 15;
    $page1->setFont($boldFont, $tableHeaderFont);

    $columnHeaderPadding = 5;
    $productColumnSize = 100;
    $previewColumnSize = 100;
    $imagePreviewSize = 100;

    $previewTableHeaderPosition = $columnHeaderPadding+$productColumnSize+$contentLeft;
    $commentTableHeaderPosition = $previewTableHeaderPosition+$previewColumnSize+$columnHeaderPadding;
    $productTableHeaderPosition = columnHeaderPadding+$contentLeft;

    $commentColumnSize = $page1->getWidth()-$commentTableHeaderPosition ;

    $page1->drawText('Product', $productTableHeaderPosition ,$contentTopFreeSpace);
    $page1->drawText('Preview', $previewTableHeaderPosition,$contentTopFreeSpace);
    $page1->drawText('Comments', $commentTableHeaderPosition,$contentTopFreeSpace);

    $paddingRowHeaderFromLine = 5;
    $contentTopFreeSpace = $contentTopFreeSpace-$paddingRowHeaderFromLine;
       $tableStart = $contentTopFreeSpace;

    $contentFont = 15;
    $rowHeight= 125;

    $page1->setFont($regularFont, $contentFont);
    foreach($wishListItemCollection as $w)
    { 
        $page1->drawLine($contentLeft,$contentTopFreeSpace ,$contentRight,$contentTopFreeSpace);
        $contentTopFreeSpace =$contentTopFreeSpace- $contentFont-$paddingRowHeaderFromLine;

        $product = $w->getProduct(); 
        $product = Mage::getModel('catalog/product')->load($product->getId());
        $page1->drawTextBlock($product->getName(), $productTableHeaderPosition,$contentTopFreeSpace,$productColumnSize);
        $page1->drawTextBlock($w->getDescription(), $commentTableHeaderPosition,$contentTopFreeSpace,$commentColumnSize);

        $imagePath = $this->getImagePath($product);

        // Load image
        $image = Zend_Pdf_Image::imageWithPath($imagePath); 

        $imageObj = new Varien_Image($imagePath); 

        $imageSizes = $this->getImageSizesWithAspectRatio($imageObj->getOriginalWidth(),$imageObj->getOriginalHeight(),$imagePreviewSize,$rowHeight);

        $imageBottom = ($rowHeight-$imageSizes['height'])+$contentTopFreeSpace;
        $imageLeft = $previewTableHeaderPosition;
        $page1->drawImage($image,$imageLeft,$imageBottom-$imageSizes['height'],$previewTableHeaderPosition+$imageSizes['width'],$contentTopFreeSpace);
        $contentTopFreeSpace = $contentTopFreeSpace-$rowHeight-$paddingRowHeaderFromLine;
        $page1->drawLine($contentLeft,$contentTopFreeSpace ,$contentRight,$contentTopFreeSpace);
    };

    $tableLineProductsStartX =  $previewTableHeaderPosition-$columnHeaderPadding;
    $tableLineProductsStartY = $tableStart+$paddingRowHeaderFromLine+$tableHeaderFont;
    $tableLineProductsStartX1 =  $previewTableHeaderPosition-$columnHeaderPadding;
    $tableLineProductsStartY1 = $contentTopFreeSpace;

    $tableLinePreviewStartX =  $commentTableHeaderPosition-$columnHeaderPadding;
    $tableLinePreviewStartY = $tableStart+$paddingRowHeaderFromLine+$tableHeaderFont;
    $tableLinePreviewStartX1 =  $commentTableHeaderPosition-$columnHeaderPadding;
    $tableLinePreviewStartY1 = $contentTopFreeSpace;

    $page1->drawLine($tableLineProductsStartX ,$tableLineProductsStartY  ,$tableLineProductsStartX1,$tableLineProductsStartY1);
    $page1->drawLine($tableLinePreviewStartX ,$tableLinePreviewStartY  ,$tableLinePreviewStartX1,$tableLinePreviewStartY1);
    return $page1;
}

资料来源: http: //gorrc.blogspot.in/2012/05/magento-print-whishlist-to-pdf.html

于 2012-08-08T21:12:15.917 回答