I want to use the tFPDF class and load it with composer-autoload. Since I could not find an official tFPDF composer repo, I simply downloaded the zip file and extracted it in the folder vendor/tfpdf.
Next I added the psr-4 to the composer.json file:
"autoload": {
"psr-4": {
"App\\": "app/",
"tFPDF\\": "vendor/tFPDF"
},
I also added in the tfpdf.php the namespace
<?php
namespace tFPDF;
define('tFPDF_VERSION','1.25');
class tFPDF
{
Finally I produced a new autoload file:
composer dump-autoload
When I now try to create a PDF like this:
$pdf = new \tFPDF\tfpdf();
$pdf->AddPage();
// Add a Unicode font (uses UTF-8)
$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true);
Then the last line will rise an error:
Cannot access private property tFPDF\TTFontFile::$charWidths
Why is that and how can I fix it?
There is now a package https://github.com/Setasign/tfpdf (released in September 2018, a couple of month after the question was asked here)
Install with composer like this:
composer require setasign/tfpdf
you can either use it like this:
namespace your\namespace;
class Document extends \tFPDF
or like this:
$pdf = new \tFPDF();
Related
i have maybe primitive problem. I created my first package in composer. It is just one class in one namespace.
composer.json:
...
"autoload": {
"psr-4": {
"UrlParser\\": "src/"
}
},
...
and i have this in: src/UrlParser/url.php
<?php
namespace UrlParser;
class Url{
...
everything is OK, i uploaded my package into composer. I install it into my project, but when i call this:
<?php
require_once 'vendor/autoload.php';
$a = new UrlParser\Url("http://localhost/aaa.html");
i get this: Fatal error: Class 'UrlParser\Url' not found in C:\xampp\htdocs\ccc\01\index.php on line 3
I am new in composer and i try to google my problem, but i am lost :)
Thanks
Try this
namespace UrlParser;
$a = new Url("http://localhost/aaa.html");
If it doesn't work, there's probably a problem with autoload that you didn't get the right way
the problem was, that i didnĀ“t do this:
composer dump-autoload -o
I'm trying to install dompdf using composer, I followed the instructions from Installing DOMPDF with Composer
So far I've
In composer.json
...
"require": {
...
"dompdf/dompdf": "~0.6.1"
},
"autoload": {
....
run composer update
in autoload.php already have require __DIR__.'/../vendor/autoload.php';
In vendor/dompdf/dompdf/dompdf_config.inc.php
changed def("DOMPDF_ENABLE_AUTOLOAD", true); to def("DOMPDF_ENABLE_AUTOLOAD", false);
My controller code
```
use Dompdf\Adapter\CPDF;
use Dompdf\Dompdf;
use Dompdf\Exception;
require_once "vendor/dompdf/dompdf/dompdf_config.inc.php";
class ArticleController extends BaseController {
...
public function downloadPdf(){
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
$dompdf->render();
$dompdf->output();
}
}
"post" route for ArticleController#downloadPdf
so now when I try to download pdf, if gives me error:
exception 'Symfony\Component\Debug\Exception\FatalErrorException' with
message 'Class 'Dompdf\Dompdf' not found'
have I missed any setup step or doing something wrong?
I don't think that you want use the dompdf 0.6 family. In the 0.6 version all the classes are in global space. But since your code is ready to the 0.7, change it to
"dompdf/dompdf": "~0.7"
and run composer update.
This issue at dompdf github page helped me to solve this error
The latest stable (0.6.1) does not support namespaces and so would not need the use statement in your code. The upcoming release (0.7.0) does include namespace support.
So, I just removed
use Dompdf\Adapter\CPDF;
use Dompdf\Dompdf;
use Dompdf\Exception;
and used new DOMPDF(); instead of new Dompdf(); as with version 0.6.* namespace will not work.
Open file config.php
application/config/config.php
Then Change
$config['composer_autoload'] = "FALSE";
To
$config['composer_autoload'] = "vendor/autoload.php";
I have THIS package (a payment gateway), which I would like to use in Symfony 3.0.1
Unfortunately I get this error:
ClassNotFoundException in AppKernel.php line 21: Attempted to load class "SofortBundle" from namespace "Sofort\SofortLib".
Did you forget a "use" statement for another namespace?
In the sofort\sofortlib-php folder i created the file SofortBundle.php with this content
<?php
namespace Sofort\SofortLib;
use Symfony\Component\HttpKernel\Bundle\Bundle as BaseBundle;
class SofortBundle extends BaseBundle
{
}
and I loaded the Bundle in AppKernel.php:
new Sofort\SofortLib\SofortBundle(),
But that only leads to above exception.
What am I missing?
Don't copy packages to your custom folder. Install package as described:
In composer.json add:
"require": {
"sofort/sofortlib-php": "3.*"
}
Run composer update sofort/sofortlib-php
In your code you can use the library like this:
use \Sofort\SofortLib\Billcode;
class MyClass
{
function doSomething($configkey) {
$SofortLibBillcode = new Billcode($configkey);
...
}
}
I scrapped the earlier form of my question because it was too convoluted. Here's the new version.
I want to use phpspec with my psr-4 formatted projects.
Here's the way I tried to set up a test project:
Created a new folder for the project:
cd ~/Desktop/
mkdir TestPhpSpec
cd TestPhpSpec
create a new composer.json file and require phpspec:
composer require phpspec/phpspec
Which creates my composer.json file:
{
"require": {
"phpspec/phpspec": "^2.3"
}
}
I add my psr-4 namespace to the autoload property of my composer.json file:
{
"require": {
"phpspec/phpspec": "^2.3"
},
"autoload": {
"psr-4": {
"Acme\\": "src/Acme"
}
}
}
Then I dump my autoload to make sure my namespace is loaded: composer dumpautoload
After that, I create my phpspec.yml to describe the namespace to phpspec:
suites:
acme_suite:
namespace: Acme
psr4_prefix: Acme
Then I describe the class I want to start building:
phpspec describe Acme/Markdown
This is where I run into the first problem. Even though I specify the Acme namespace in my describe command, the spec does not get placed in a folder matching the namespace:
Though the class it creates is namespaced correctly:
<?php
namespace spec\Acme; // correct namespace
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class MarkdownSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Acme\Markdown');
}
}
Then if I try to run the test to start TDD-ing.
phpspec run
It offers to create the class for me and I let it. From there I get the second problem; I get the error message:
[PhpSpec\Process\Prerequisites\PrerequisiteFailedException]
The type Acme\Markdown was generated but could not be loaded. Do you need to configure an autoloader?
And the class it creates is not in it's namespaced folder:
The class it creates is also namespaced correctly:
<?php
namespace Acme; // correct namespace
class Markdown
{
}
I've looked over the docs and can't figure out what I'm doing wrong. Any suggestions?
Try with
suites:
acme_suite:
src_path: Acme/src
spec_path: Acme/spec
Hello guys i have problem with autoloading my class with composer. On Linux all work perfect, but now my boss change env and set Windows. All this work on linux but windows show newbie fatal error:
Fatal error: Class 'AbstractController' not found in
D:\xampp\htdocs\ikacFw\frontController.php on line 7
Common to see my composer.json and stucture for better picture on problem.
Stucture is :
frontController.php
-- vendor
----- Doctrine
----- Ikac
--------- Components
---------- Mvc
------------- Controller
Am trying to load all data from vendor directory.
Composer.json
{
"autoload": {
"psr-0": {
"vendor": ""
}
}
}
Also new component i add manual. Like this :
$loader = require_once 'vendor/autoload.php';
$loader->add('vendor', "Ikac");
Okay next when i try to call :
<?php
require_once 'vendor/autoload.php';
use Ikac\Mvc\Controller;
$a = new AbstractController();
I get error "not found".
My class AbstractController contain defined namespace but dont work again. Like test i do this:
<?php
//vendor/Ikac/Mvc/Controller/AbstractController.php
namespace Ikac\Mvc\Controller;
class AbstractController {
function __construct() {
echo __CLASS__;
}
}
?>
I do from cmd composer dump-autoload, install, but dont work. All this perfect work on linux but here wont. Any idea how to fix this or where i do mistake.
Thanks guys!
SLOVED:
{
"autoload": {
"psr-0": {
"": "vendor/"
}
}
}
Well you should do
<?php
require_once 'vendor/autoload.php';
use Ikac\Mvc\Controller\AbstractController;
$a = new AbstractController();
Your autoloading declaration is wrong.
You will NEVER ever need to include the vendor folder in any autoloading. The vendor folder will contain the autoloading for both all dependencies, and - if configured - for your own classes as well.
You can use Composer to create autoloading for your own classes. Just include the correct info. But from your current info I cannot deduct what would be correct.