PrestaShop libraries replacement - php

I use PrestaShop 1.6.1.4 and I want to change the library tcpdf with dompdf.
I use this form for creating invoices.
What are the best practices for a library exchange?

I created inside override the tools folder and I put us dompdf-master found here https://github.com/dompdf/dompdf.
Instead inside override/classes/pdf i copied PDFGenerator.php, there is in classes/pdf.
In PDFGenerator.php add:
require_once('/../override/tools/dompdf-master/dompdf/Dompdf.php');
require_once('/../override/tools/dompdf-master/autoload.inc.php');
include('/../override/tools/dompdf-master/dompdf/dompdf_config.inc.php');
use Dompdf\Dompdf;
use Dompdf\Options;
The class becomes:
class PDFGenerator extends DOMPDF
Eliminates the render() function and replace it with:
public function render($filename, $display = true)
{
if (empty($filename)) {
throw new PrestaShopException('Missing filename.');
}
$html = $this->header.$this->content.$this->footer;
//die($html);
$options = new Options();
$options->set('A4','potrait');
$options->set('enable_css_float',true);
$options->set('isHtml5ParserEnabled', true);
$dompdf = new DOMPDF($options);
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($filename);
}
Then i deleted cache/class_index.php

Related

Unable to extract the content of pdf file in php

Currently working on validating the pdf file. I have used PHP pdfparser in Laravel to extract the file. But some files are unable to extract. I come up with the solution to downgrade the pdf file to resolve the issue but still not working for me.
I tried to downgrade the pdf file from version 1.7 to 1.4 but it's not allowing me to do so.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Smalot\PdfParser\Parser;
use Xthiago\PDFVersionConverter\Guesser\RegexGuesser;
use Symfony\Component\Filesystem\Filesystem;
use Xthiago\PDFVersionConverter\Converter\GhostscriptConverterCommand;
use Xthiago\PDFVersionConverter\Converter\GhostscriptConverter;
class ApiController extends Controller {
public function varifyDocument(Request $request, Parser $parser) {
$request = $request->file();
$file = $request['file'];
//Get the version of the pdf file.
$guesser = new RegexGuesser();
$version = $guesser->guess($file);
//If pdf version is 1.7 then convert it to 1.4.
if($version == "1.7") {
$command = new GhostscriptConverterCommand();
$filesystem = new Filesystem();
$converter = new GhostscriptConverter($command, $filesystem);
$converter->convert($file, '1.4');
}
$pdf = $parser->parseFile($file);
$pages = $pdf->getPages();
foreach ($pages as $page) {
echo $page->getText();
}
}
}
I need to read the content of the pdf file and identify it has any vulnerability or not.

Unable to convert an excel to pdf using php

I have installed PhpSpreadsheet and dompdf successfully using composer.
My requirement is that I need to convert an excel sheet into pdf, I got it working using the default settings, this is the code I have used.
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Writer\Csv;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\IOFactory;
use \PhpOffice\PhpSpreadsheet\Writer\Pdf\Dompdf;
$spreadsheet = new Spreadsheet();
try {
$sheet = $spreadsheet->getActiveSheet();
// code to fill in the data
$spreadsheet->getActiveSheet()->fromArray(
$data, // The data to set
NULL, // Array values with this value will not be set
'A2' // Top left coordinate of the worksheet range where
);
} catch (Exception $e) {
}
$writer = new Xlsx($spreadsheet);
try {
IOFactory::registerWriter("PDF", Dompdf::class);
$pdfwriter = IOFactory::createWriter($spreadsheet, 'PDF');
$pdfwriter->save($filepath . 'pdf_test.pdf');
} catch (\PhpOffice\PhpSpreadsheet\Writer\Exception $e) {
}
I have skipped out code for brevity, this code works fine and generates a pdf file, I require the pdf to be printed in landscape mode, for that the docs mention a Custom implementation or configuration of the pdf library, so I created a file called PDFBase_DOMPDF that looks like this
use Dompdf\Dompdf;
class PDFBase_DOMPDF extends Dompdf
{
}
And I have created a file called PDFBase_Writer that looks like this.
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Dompdf;
class PDFBase_Writer extends Dompdf
{
protected function createExternalWriterInstance()
{
$instance = new PDFBase_DOMPDF();
$instance->setPaper('A4', 'landscape');
return $instance;
}
}
I modified the original code to use the new pdf class so the line changed to this.
IOFactory::registerWriter("PDF", PDFBase_Writer::class);
The problem is I get an exception with the following error
Registered writers must implement PhpOffice\PhpSpreadsheet\Writer\IWriter
How exactly do I fix this?
Reading and writing to a persisted storage is not possible using the base PhpSpreadsheet classes. For this purpose, PhpSpreadsheet provides readers and writers, which are implementations of \PhpOffice\PhpSpreadsheet\Reader\IReader and \PhpOffice\PhpSpreadsheet\Writer\IWriter.
You must load the Excel file like this:
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load("TestRead.xlsx");
Registered writers must implement PhpOffice\PhpSpreadsheet\Writer\IWriter
PHPSpreadsheet Writer classes must implement all the methods defined in the IWriter interface. You're creating a new Writer, so it needs to provide an implementation of all those methods:
interface IWriter
{
/**
* IWriter constructor.
*
* #param Spreadsheet $spreadsheet
*/
public function __construct(Spreadsheet $spreadsheet);
/**
* Save PhpSpreadsheet to file.
*
* #param string $pFilename Name of the file to save
*
* #throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
public function save($pFilename);
}
So your Writer needs to implement a constructor that accepts a Spreadsheet object as an argument, and a save() method that accepts a filename (as a string) argument.

DOMPDF Doesn't Work if HTML is Already Present

I have a function that uses DOMPDF to generate a .pdf of whatever I set for the $html argument.
function generate_pdf($html){
//DOMPDF stuff
}
This works, but the problem I'm running into is that when I call this function from a page that already has HTML content, it fails.
Fails...
<?php
require_once '../header.php'; //This has HTML content in it.
$html = '<h1>stuff</h1>';
generate_pdf($html);
?>
This also fails...
<?php
echo 'stuff';
$html = '<h1>stuff</h1>';
generate_pdf($html);
?>
Works...
<?php
$html = '<h1>stuff</h1>';
generate_pdf($html);
?>
Is there any way around this?
Contents of function generate_pdf($html)
function generate_pdf($html){
//Get the necessary dompdf files
include_once DOMPDF_PATH . '/autoload.inc.php';
// instantiate and use the dompdf class
$dompdf = new \Dompdf\Dompdf();
$dompdf->loadHtml($html);
// Render the HTML as PDF
$dompdf->render();
//Output the PDF
$dompdf->stream();
}
Note that the file in which this function lives has a namespace, so that's why $dompdf = new \Dompdf\Dompdf(); might appear wrong, but that line is working fine.
CHANGED ANSWER after full code was added to question:
I have the complete HTML content in an included file, no extra HTML before or after it (also no php echoing).
I made a mistake before - I mixed up the inclusion of the dompdf autoload and the content - but see below how it works in my case. This is the content of a php file, there is no function:
<?php
require_once '../../dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->set_option('isPhpEnabled', true);
ob_start();
//Here I am getting a few variables (from the URL via GET) which I use in the included php file
include_once "your_content_in_one_file.php";
$html = ob_get_contents();
ob_end_clean();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream($pdf_name);
?>
It ended up being related to the fact that ob_start(); had already been called somewhere else in the system. Using...
if (ob_get_level()){
ob_end_clean();
}
fixed my issue. Hope it helps someone else.

How to add logo to stripe invoice pdf in laravel

How to add logo to stripe invoice pdf. I'm using laravel framework. Is there any way I can modify the blade file under vendor section? and commit the same.
Run php artisan vendor:publish.
This will create vendor/cashier/receipt.blade.php file in your views folder, which you can modify as per your requirement & commit.
Extend Cashier's Invoice - Laravel\Cashier\Invoice.
Add $options->setIsRemoteEnabled(true); to pdf method:
public function pdf(array $data)
{
if (! defined('DOMPDF_ENABLE_AUTOLOAD')) {
define('DOMPDF_ENABLE_AUTOLOAD', false);
}
$options = new Options;
$options->setChroot(base_path());
$options->setIsRemoteEnabled(true);
$dompdf = new Dompdf($options);
$dompdf->setPaper(config('cashier.paper', 'letter'));
$dompdf->loadHtml($this->view($data)->render());
$dompdf->render();
return $dompdf->output();
}

PDF data displayed but file not downloaded

I want to preview and download an order invoice pdf using this code :
public function generatePDFByIdOrder()
{
$order = new Order(1); //I want to download the invoice PDF of $order_id '1'
if (!Validate::isLoadedObject($order)) {
throw new PrestaShopException('Can\'t load Order object');
}
$order_invoice_collection = $order->getInvoicesCollection();
$this->generatePDF($order_invoice_collection, PDF::TEMPLATE_DELIVERY_SLIP);
}
public function generatePDF($object, $template)
{
$pdf = new PDF($object, $template, Context::getContext()->smarty);
$pdf->render();
}
And calling it with the following code :
$order = new order();
echo $order->generatePDFByIdOrder();
I have the pdf's data printed on the browser console but not downloaded .
How can I manipulate that data to download a pdf file ?
PrestaShop use TCPDF.
Edit generatePDF in this way:
public function generatePDF($object, $template)
{
$pdf = new PDF($object, $template, Context::getContext()->smarty);
$pdf->Output('name.pdf', 'I');
}
I guess you only have to set the proper headers before rendering the PDF with TCPDF like so :
header("Content-type:application/pdf");
But "downloading" a PDF will depend on what the user's browser settings. It might download them (in which case you'd have to set another header called Content-Disposition:attachment) or display it inside the browser.
We recommend you, to create a separate controller to render the PDF file and to always open that controller in a new tab. It will help you to have separate logic using DOMPDF library.
Invoice controller will be as follows (invoice.php)
include_once(_PS_MODULE_DIR_.'supercehckout/libraries/dompdf/dompdf_config.inc.php');
class SuperCheckoutInvoiceModuleFrontController extends ModuleFrontController
{
public function initContent()
{
parent::initContent();
$this->generateInvoice(ORDER_ID);
}
}
Note: SuperCheckout is the example module name.
generateInvoice() function will be as follows:
function generateInvoice($order_id)
{
$dompdf = new DOMPDF();
$html = utf8_decode(INVOICE_HTML);
$dompdf->load_html(INVOICE_HTML);
$dompdf->render();
}

Categories