1

I am trying to print logo into custom pdf through controller. I am able to print product name, sku etc.But I don't know how to add store logo. My code is as below. Can anyone please help me to add store logo into custom pdf as I have logo URL but don't know how to insert them. Thanks in advance.

$logoURl = $this->_logo->getLogoSrc(); //logo URL
$productName = $product->getName(); //Product Name
$productSku = $product->getSku(); //Product SKU

$pdf = new \Zend_Pdf();
$page = $pdf->newPage(\Zend_Pdf_Page::SIZE_A4);
$pdf->pages[] = $page;
$page->setFont(\Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_HELVETICA), 20);
$page->drawText($logoURl, 10, 500); //Able to print Logo URL
$page->drawText($productName, 10, 400); //Able to print product name
$page->drawText($productSku, 10, 350); //Able to print product SKU
$pdfData = $pdf->render();

header("Content-Disposition: inline; filename=result.pdf");
header("Content-type: application/x-pdf");

echo $pdfData;

3 Answers 3

1

You should use drawImage instead of drawText function.

Try :

    $folderName = \Magento\Config\Model\Config\Backend\Image\Logo::UPLOAD_DIR;
    $storeLogoPath = $this->_scopeConfig->getValue(
        'design/header/mobile_logo_src',
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );
    $path = $folderName . '/' . $storeLogoPath;
    $logoUrl = $this->_urlBuilder
            ->getBaseUrl(['_type' => \Magento\Framework\UrlInterface::URL_TYPE_MEDIA]) . $path;
    if ($logoUrl) {
        if (is_file($logoUrl)) {
           $pdfImage = Zend_Pdf_Image::imageWithPath($logoUrl);
           $page->drawImage($pdfImage, 10, 500);
        }
    }
8
  • Hi Vinz, thanks for your answer. I used above code and I get "Cannot create image resource. File not found" error. Can you please suggest what needs to be done. Jan 29, 2019 at 17:37
  • It seems that your $logoURl is not good. Are you sure that your path is the right ?
    – Vinz
    Jan 29, 2019 at 18:12
  • Yes path is correct because when I echo $logoURL and check same URL in browser, I am able to access logo with $logoURL Jan 29, 2019 at 18:44
  • I have updated my answer
    – Vinz
    Jan 29, 2019 at 19:16
  • 1
    @HaFizUmer It's config created, to try it you can set your path in $storeLogoPath.
    – Vinz
    Jul 29, 2019 at 14:44
0

After spending sometime, I got to know to print logo into custom pdf using logoURL. My mistake was that I was considering relative path of logo URL and not the application path. Hence I was getting error. For exam : If I am working on ubunto machine localhost and my project folder name is abc with logo.png inside it just in directory where there is app folder. Then I should consider path as "/var/www/html/abc/logo.png" and not "http://127.0.0.1/abc/logo.png" I changed path to /var/www/html/abc/logo.png and was able to print logo into custom pdf.

Writing piece of code lines here below to understand better

$pdfImage = \Zend_Pdf_Image::imageWithPath("/var/www/html/abc/logo.png");

$page->drawImage($pdfImage, 10, 600);

0

Try This

use Magento\Framework\App\Filesystem\DirectoryList;

protected $scopeConfig;

protected $fileFactory;

public function __construct(
    ................................................>
    \Magento\Framework\Filesystem $filesystem,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
    ................................................>
) {
    ................................................>
    $this->_mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
    $this->scopeConfig = $scopeConfig;
    $this->fileFactory = $fileFactory;
    ................................................>
}

public function execute()
{
    $pdf = new \Zend_Pdf();
    // $zendImage = new \Zend_Pdf_Image();
    $pdf->pages[] = $pdf->newPage(\Zend_Pdf_Page::SIZE_A4);
    $page = $pdf->pages[0]; // this will get reference to the first page.
    $style = new \Zend_Pdf_Style();
    $style->setLineColor(new \Zend_Pdf_Color_Rgb(0, 0, 0));
    $font = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_TIMES);

    $width = $page->getWidth();
    $hight = $page->getHeight();
    $x = 30;
    $pageTopalign = 850; //default PDF page height
    $this->y = 850 - 100; //print table row from page top – 100px

    $style->setFont($font, 10);
    $page->setStyle($style);

    $folderName = \Magento\Config\Model\Config\Backend\Image\Logo::UPLOAD_DIR;

    $path = $folderName . '/logo.jpg';

    $pdfImage = \Zend_Pdf_Image::imageWithPath($this->_mediaDirectory->getAbsolutePath($path));

    $top = 800;
    //top border of the page
    $widthLimit = 270;
    //half of the page width
    $heightLimit = 265;
    //assuming the image is not a "skyscraper"
    $width = $pdfImage->getPixelWidth();
    $height = $pdfImage->getPixelHeight();

    //preserving aspect ratio (proportions)
    $ratio = $width / $height;
    if ($ratio > 1 && $width > $widthLimit) {
        $width = $widthLimit;
        $height = $width / $ratio;
    } elseif ($ratio < 1 && $height > $heightLimit) {
        $height = $heightLimit;
        $width = $height * $ratio;
    } elseif ($ratio == 1 && $height > $heightLimit) {
        $height = $heightLimit;
        $width = $widthLimit;
    }

    $y1 = $top - $height;
    $y2 = $top;
    $x1 = 30;
    $x2 = $x1 + $width;

    $page->drawImage($pdfImage, $x1, $y1, $x2, $y2);

    $fileName = 'example.pdf';

    header("Content-Disposition: inline; filename=result.pdf");

    header("Content-type: application/x-pdf");

    // echo $pdf->render();

    $this->fileFactory->create(
        $fileName,
        $pdf->render(),
        \Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR,
        'application/pdf'
    );
}

I Hope This Helps You.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.