The question:
I am adding one custom pdf functionality in my magento store, for that I want to use TCPDF but I am not able to add it. it always shows that class is not found and all.
Can anyone tell me what is proper way to add and use tcpdf in magento 2 ?
Thanks in advance.
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
Try to run following command from M2 root directory
composer require tecnickcom/tcpdf
After install, use following way to create new PDF document tcpdf
// create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
Method 2
Here is the solution to install TCPDF without composer ;
-
Download TCPDF from https://github.com/tecnickcom/tcpdf
-
Change the name of the file root directory after extraction to TCPDF
-
Inside this directory, there is a tcpdf.php file, change the name to TCPDF
-
Open the file and change the class name from tcpdf to
TCPDF_TCPDF
, am very sure those that are really familiar with Magento will know why. -
Copy the files into your Magento “lib” folder in the root Magento installation directory.
-
And you have successfully incorporated TCPDF with Magento, from anywhere in Magento, you can call this class by
$tcpdf = new TCPDF_TCPDF()
.
Working tested in magento 2.1.10.
Reference – https://edmondscommerce.github.io/magento/using-tcpdf-with-magento.html
Method 3
For that you can add "tecnickcom/tcpdf": "*"
in your root composer file. here tecnickcom/tcpdf
is TCPDF
library and *
contain the latest version of TCPDF
. If you want to use any specific version TCPDF
then replace with *
with version number.
After updating the composer file you just need to run composer update
command from magento 2 root directory. After successfully downloading TCPDF
you can use in every place of Magento 2 like block
,helper
etc
here is the my composer file
{
"name": "magento/project-community-edition",
"description": "eCommerce Platform for Growth (Community Edition)",
"type": "project",
"version": "2.1.2",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"require": {
"magento/product-community-edition": "2.1.2",
"composer/composer": "@alpha",
"tecnickcom/tcpdf": "*"
},
"require-dev": {
"phpunit/phpunit": "4.1.0",
"squizlabs/php_codesniffer": "1.5.3",
"phpmd/phpmd": "@stable",
"pdepend/pdepend": "2.2.2",
"fabpot/php-cs-fixer": "~1.2",
"lusitanian/oauth": "~0.3 <=0.7.0",
"sebastian/phpcpd": "2.0.0"
},
"config": {
"use-include-path": true
},
"autoload": {
"psr-4": {
"Magento\Framework\": "lib/internal/Magento/Framework/",
"Magento\Setup\": "setup/src/Magento/Setup/",
"Magento\": "app/code/Magento/"
},
"psr-0": {
"": "app/code/"
},
"files": [
"app/etc/NonComposerComponentRegistration.php"
]
},
"autoload-dev": {
"psr-4": {
"Magento\Sniffs\": "dev/tests/static/framework/Magento/Sniffs/",
"Magento\Tools\": "dev/tools/Magento/Tools/",
"Magento\Tools\Sanity\": "dev/build/publication/sanity/Magento/Tools/Sanity/",
"Magento\TestFramework\Inspection\": "dev/tests/static/framework/Magento/TestFramework/Inspection/",
"Magento\TestFramework\Utility\": "dev/tests/static/framework/Magento/TestFramework/Utility/"
}
},
"minimum-stability": "alpha",
"prefer-stable": true,
"repositories": [
{
"type": "composer",
"url": "https://repo.magento.com/"
}
],
"extra": {
"magento-force": "override"
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0