FPDF with Laravel - php

I am attempting to use the FPDF (http://www.fpdf.org/) PHP addon for my website that uses Laravel. I want to be able to dynamically create a PDF.
I have stored the libraries' files in the folder: '/public/vendor'.
This is what I have so far:
require $_SERVER['DOCUMENT_ROOT'] . '/vendor/fpdf/fpdf.php';
which works fine. However when I try to use the FPDF class using:
$pdf = new FPDF('P', 'pt', array(500,233));
I get the error: Class "'App\Console\Commands\FPDF' not found"
How can I fix this to use the library. I do not have access to command line so I have to manually import any folders or files.
Any help is greatly appreciated.
Update:: I do not have any access to the console so i cannot use composer. Any way to still do this?

Ideally if using Laravel, you should load it via composer the get it to work correctly.
If you do not have access to composer on the server, you could download the project, make sure you are running the same version of PHP as the server and run composer install fpdf/fpdf. Re-upload the composer.json, composer.lock and vendor folder.
https://packagist.org/packages/fpdf/fpdf

Try following code:
$pdf = new \Fpdf\Fpdf('P', 'pt', array(500,233));
FPDF is in the root namespace and you are within another namespace.

Highly recommend using https://github.com/mccarlosen/laravel-mpdf which can be installed via composer
It supports blade views and you can pass variables to it just like in view()

Related

How to use Mpdf library with PHPword. How can I generate .docx file to .pdf file

I have a problem. When I try to generate the word.docx file to pdfFile.pdf file on Laravel (PHP), using the library mPDF I got this error:
PDF rendering library or library path has not been defined.
I can't found the solution. Help me please. Thank you in advance.
My code in PHP controller file:
public function wordToPdf(){
$domPdfPath = base_path('vendor/mpdf/mpdf');
\PhpOffice\PhpWord\Settings::setPdfRendererPath($domPdfPath);
\PhpOffice\PhpWord\Settings::setPdfRendererName('mPDF');
//Load word file
$Content = \PhpOffice\PhpWord\IOFactory::load(public_path( '/uploads/word/no12.docx'));
//Save it into PDF
$PDFWriter = \PhpOffice\PhpWord\IOFactory::createWriter($Content,'PDF');
$PDFWriter->save(public_path( '/uploads/word/result3.pdf'));
return $PDFWriter;
}
Help me please if anyone know it..?
I had the same issue but with different library, It was because, the Laravel could not find the base path of your PDF rendering library and it is pretty clear in your error.
All you have to do is define a path to your domPDF. A better approach would be to define a constant and then access it. Here's how you do it:
Create a file in your config folder by any name you want, constants.php would be a good practice since you're defining constants for your app.
Define a constant in that file 'your_var_name' => realpath(__DIR__),.
Run these commands in your cmd: php artisan config:cache and php artisan cache:clear to clear and cache the latest configs.
Now in your controller setup the following settings:
Code:
// access your constant using laravel's helper config();
$domPdfPath = realpath(config('constant.your_var_name').'/../vendor/dompdf/dompdf');
Settings::setPdfRendererPath($domPdfPath);
Settings::setPdfRendererName('DomPDF');

How to use PhpSpreadsheet without installation (like PHPExcel)

According to the PhpSpreadsheet Doc it's neccessary to install it with composer.
In my case I just have a webspace without Terminal but Plesk. Is it anyway possible to use PhpSpreadsheet, like it is with PHPExcel where you just have to place the files in any location?
What do I have to do to get it run? I found no further information how to with only FTP webserver access.
In your case there are two options for you!
Answer: 1
Alternative method without terminal
Run composer with a PHP script in browser
Answer: 2
Third party sites, which allow to download composer packages online. get PHPspreadsheet latest version.
https://php-download.com/package/phpoffice/phpspreadsheet
Bonus You can download almost any composer packages # https://php-download.com
Have you considered installing composer locally, running it as instructed, and then just sending the generated files to the server via normal FTP? You install composer on your computer and run it there, then upload via FTP/SFP...
Composer is not "necessarily" intended to run live while the user is downloading the pages, it is intended to be run "mainly" on your local computer and generating the files and dependencies that later on you upload to the server. It can update the files on your server, but that is a convenience not a necessity.
By registering custom autoloader and PSR simplecache autoloader it is possible to fully workaround composer installation - see:
https://github.com/PHPOffice/PhpSpreadsheet/issues/31#issuecomment-354502740
Please note, that installation via composer is currently the only officially supported solution.
I had the same problem. I downloaded Library on github from
https://github.com/PHPOffice/PhpSpreadsheet
and made a few changes
changed its namespaces
used autoload.php file
It worked
the Library without the need of Composer is in this link
https://github.com/YoloZoloo/PhpSpreadSheet/tree/master
You can change the folder name to any folder you like.
Load [AnyFolder]->table.php from your server and press 「ダウンロード」.
Hit back to me if you encounter any issues
sample code
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
Smartpal, thanks for your pack, it works fine!
Also it possible to replace folder:
/phpspreadsheet/vendor/phpoffice/phpspreadsheet/src/
in Smartpal's archive with more fresh sources from phpspreadsheet github and it also will work.
PS: If you have issues with reading xls files saved in encoding other than CP1252, you have to seek literals CP1252 in file ./src/PhpSpreadsheet/Reader/Xls.php and replace them with other value that you need or some named constant or even mb_detect_encoding(). Then it will read such xls correctly.

mPDF : Methods with the same name as their class will not be constructors in a future version of PHP; mPDF has a deprecated constructor

I'm new to mpdf and I'm having a problem implementing it on my project. I saw a link in Facebook that has a tutorial for mpdf, I read and followed all the instructions but unfortunately there is still an error about some deprecated value.
Here is the link I followed : http://codesquery.com/convert-html-pdf-codeiniter-using-mpdf/
And here is the screenshot of the problem:
I hope you guys can help me.
I guess you use php 7. You must change names of function. In old php for example 5.19 the same name of function and class means that this function is a constructor. In PHP 7 it must be for example:
public function __construct(mPDF $mpdf){}
Click here to see a list of all modification
You can easily install mpdf by using composer in codeigniter. This is how I implemented composer in CodeIgniter 3.It is very easy. You have to install composer on your machine download it from https://getcomposer.org/. After installing composer in your pc,
Copy and paste composer.json file in the project folder to application folder.
In the config.php file $config['composer_autoload'] = TRUE;
Now you have composer in your project. Now i will saw you how to install mpdf using composer
Open cmd and direct to application folder
Inside application directory Type composer require mpdf/mpdf
Now a vendor folder will be created inside application folder and inside vendor folder you can see all your packages downloaded by composer.
Now since you autoloaded composer now you can just use the code given by mpdf official manual like in your controllers.
function m_pdf(){
$mpdf = new mPDF();
// Write some HTML code:
$mpdf->WriteHTML('Hello World');
// Output a PDF file directly to the browser
$mpdf->Output();
}
Remember you don't need to type require_once APPPATH.'/vendor/mpdf/mpdf/mpdf.php'; since you already autoloader composer. If not prefer to autoload composer you must type require_once APPPATH.'/vendor/mpdf/mpdf/mpdf.php' at the beginning of each controllers where you use the mpdf vendor libraries. If you encountered any problems please comment below.

Use Composer module in PHP file

I want to use the following AntiXSS library in one PHP file. It is my first time using Composer, but I followed their installation steps and I installed it successfully. I downloaded the library through Composer, updated it and Composer created the vendor/ folder in my directory with the necessary files.
Now I added the following require 'vendor/autoload.php' into my PHP file. I created a new AntiXSS class but I obtain the following error:
Class AntiXSS not found in my directory on line 3.
I tried to use an absolute path instead of vendor/autoload.php but it isn't working yet and I don't know if I should do something more.
Best regards
The class is located in the voku\helper namespace. Use new \voku\helper\AntiXSS() to instantiate it or use use imports to import the namespace.
See php.net for more information about namespaces.

Error when trying to use the class using php

I found the code from internet which help me to extract pdf files to text.
this is the code that i have used.
<?php
// Include Composer autoloader if not already done.
//include 'vendor/autoload.php';
// Parse pdf file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile('document.pdf');
$text = $pdf->getText();
echo $text;
?>
when i use this method to call the relevant class it gives an error on the browser.
Fatal error: Class 'Smalot\PdfParser\Parser' not found in D:\workspace_PHP\pdfparser-master\index.php on line 7
i am using php version 5.4
if anyone can tell me the reason, it will be great help.
Thanks in advance
You haven't included the smalot PDFparser library so it could not found the class file for instance..
you find it here..
https://github.com/smalot/pdfparser
Include this in your source directory and that is it!..
You need to include the Smalot class file to your PHP file. As we see there is no include on your PHP file.
Include it something like
include('/Smalot/PdfParser/Parser.php');
See the GitHub here
PDFParser has been designed on top of composer library.
I invite you to read composer documentation to understand how to generate and use the composer autoloader.
It's really easy to use and really powerful.
Install composer (http://getcomposer.org/) and type
composer.phar install
In the same folder as the "composer.json" file.
All dependencies will be downloaded into the "vendor" folder.
Once done, a file will be generated named "vendor/autoload.php".
You have just to include it at first in your script.
http://pdfparser.org/documentation

Categories