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;
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);
        }
    }
| improve this answer | |
  • 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. – Sachin Bhave Jan 29 '19 at 17:37
  • It seems that your $logoURl is not good. Are you sure that your path is the right ? – Vinz Jan 29 '19 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 – Sachin Bhave Jan 29 '19 at 18:44
  • I have updated my answer – Vinz Jan 29 '19 at 19:16
  • 1
    @HaFizUmer It's config created, to try it you can set your path in $storeLogoPath. – Vinz Jul 29 '19 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);

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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