재정의했습니다 DefaultInvoice
class DefaultInvoice extends \Magento\Sales\Model\Order\Pdf\Items\Invoice\DefaultInvoice
{
public function draw()
{
$order = $this->getOrder();
$item = $this->getItem();
$pdf = $this->getPdf();
$page = $this->getPage();
$lines = [];
// draw Product image
$productImage = $this->getProductImage($item, $page);
// draw Product name
$lines[0] = [['text' => $this->string->split($item->getName(), 35, true, true), 'feed' => 35]];
$lines[0][] = array(
'text' => $productImage,
'is_image' => 1,
'feed' => 200
);
// draw SKU
$lines[0][] = [
'text' => $this->string->split($this->getSku($item), 17),
'feed' => 370,
'align' => 'right',
];
// draw QTY
$lines[0][] = ['text' => $item->getQty() * 1, 'feed' => 475, 'align' => 'right'];
// draw item Prices
$i = 0;
$prices = $this->getItemPricesForDisplay();
$feedPrice = 425;
$feedSubtotal = $feedPrice + 140;
foreach ($prices as $priceData) {
if (isset($priceData['label'])) {
// draw Price label
$lines[$i][] = ['text' => $priceData['label'], 'feed' => $feedPrice, 'align' => 'right'];
// draw Subtotal label
$lines[$i][] = ['text' => $priceData['label'], 'feed' => $feedSubtotal, 'align' => 'right'];
$i++;
}
// draw Price
$lines[$i][] = [
'text' => $priceData['price'],
'feed' => $feedPrice,
'font' => 'bold',
'align' => 'right',
];
// draw Subtotal
$lines[$i][] = [
'text' => $priceData['subtotal'],
'feed' => $feedSubtotal,
'font' => 'bold',
'align' => 'right',
];
$i++;
}
// draw Tax
$lines[0][] = [
'text' => $order->formatPriceTxt($item->getTaxAmount()),
'feed' => 515,
'font' => 'bold',
'align' => 'right',
];
// custom options
$options = $this->getItemOptions();
if ($options) {
foreach ($options as $option) {
// draw options label
$lines[][] = [
'text' => $this->string->split($this->filterManager->stripTags($option['label']), 40, true, true),
'font' => 'italic',
'feed' => 35,
];
if ($option['value']) {
if (isset($option['print_value'])) {
$printValue = $option['print_value'];
} else {
$printValue = $this->filterManager->stripTags($option['value']);
}
$values = explode(', ', $printValue);
foreach ($values as $value) {
$lines[][] = ['text' => $this->string->split($value, 30, true, true), 'feed' => 40];
}
}
}
}
$lineBlock = ['lines' => $lines, 'height' => 20];
$page = $pdf->drawLineBlocks($page, [$lineBlock], ['table_header' => true],1);
$this->setPage($page);
}
/*
* Return Value of custom attribute
* */
private function getProductImage($item, &$page)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productId = $item->getOrderItem()->getProductId();
$image = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
if (!is_null($image)) {
try{
$imagePath = '/catalog/product/'.$image->getSmallImage();
$filesystem = $objectManager->get('Magento\Framework\Filesystem');
$media_dir = $filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
if ($media_dir->isFile($imagePath)) {
return $media_dir->getAbsolutePath($imagePath);
}
else
return null;
}
catch (Exception $e) {
return false;
}
}
}
}
이 같은 PDF를 반환합니다. (이미지 회전 작업 :)
업데이트
변형 기반 제품의 경우 제품 구성 속성 선택 기반
class DefaultInvoice extends \Magento\Sales\Model\Order\Pdf\Items\Invoice\DefaultInvoice
{
public function draw()
{
$order = $this->getOrder();
$item = $this->getItem();
$pdf = $this->getPdf();
$page = $this->getPage();
$lines = [];
// draw Product image
$productImage = $this->getProductImage($item, $page);
// draw Product name
$lines[0] = [['text' => $this->string->split($item->getName(), 35, true, true), 'feed' => 35]];
$lines[0][] = array(
'text' => $productImage,
'is_image' => 1,
'feed' => 200
);
// draw SKU
$lines[0][] = [
'text' => $this->string->split($this->getSku($item), 17),
'feed' => 370,
'align' => 'right',
];
// draw QTY
$lines[0][] = ['text' => $item->getQty() * 1, 'feed' => 475, 'align' => 'right'];
// draw item Prices
$i = 0;
$prices = $this->getItemPricesForDisplay();
$feedPrice = 425;
$feedSubtotal = $feedPrice + 140;
foreach ($prices as $priceData) {
if (isset($priceData['label'])) {
// draw Price label
$lines[$i][] = ['text' => $priceData['label'], 'feed' => $feedPrice, 'align' => 'right'];
// draw Subtotal label
$lines[$i][] = ['text' => $priceData['label'], 'feed' => $feedSubtotal, 'align' => 'right'];
$i++;
}
// draw Price
$lines[$i][] = [
'text' => $priceData['price'],
'feed' => $feedPrice,
'font' => 'bold',
'align' => 'right',
];
// draw Subtotal
$lines[$i][] = [
'text' => $priceData['subtotal'],
'feed' => $feedSubtotal,
'font' => 'bold',
'align' => 'right',
];
$i++;
}
// draw Tax
$lines[0][] = [
'text' => $order->formatPriceTxt($item->getTaxAmount()),
'feed' => 515,
'font' => 'bold',
'align' => 'right',
];
// custom options
$options = $this->getItemOptions();
if ($options) {
foreach ($options as $option) {
// draw options label
$lines[][] = [
'text' => $this->string->split($this->filterManager->stripTags($option['label']), 40, true, true),
'font' => 'italic',
'feed' => 35,
];
if ($option['value']) {
if (isset($option['print_value'])) {
$printValue = $option['print_value'];
} else {
$printValue = $this->filterManager->stripTags($option['value']);
}
$values = explode(', ', $printValue);
foreach ($values as $value) {
$lines[][] = ['text' => $this->string->split($value, 30, true, true), 'feed' => 40];
}
}
}
}
$lineBlock = ['lines' => $lines, 'height' => 20];
$page = $pdf->drawLineBlocks($page, [$lineBlock], ['table_header' => true],1);
$this->setPage($page);
}
/*
* Return Value of custom attribute
* */
private function getProductImage($item, &$page)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productId = $item->getOrderItem()->getProductId();
$data = $objectManager->get('Magento\Catalog\Model\ProductRepository')->get($item->getSku());
$image = $data->getImage();
if (!is_null($image)) {
try{
$imagePath = '/catalog/product/'.$image;
$filesystem = $objectManager->get('Magento\Framework\Filesystem');
$media_dir = $filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
if ($media_dir->isFile($imagePath)) {
return $media_dir->getAbsolutePath($imagePath);
}
else
return null;
}
catch (Exception $e) {
return false;
}
}
}
}
구성 제품의 변형 선택을 기반으로 한 송장의 간단한 제품 이미지입니다.
추가 참조
참조 1 , 참조 2 , 참조 3