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.
Related
I made a quick controller in order to test how I can convert a docx template into pdf after replacing placeholder values. The .docx is:
https://drive.google.com/file/d/14-Nh0L-R7ulHgpmTN5s-T0PilwWSsE7L/view?usp=sharing
And I use the following laravel code to convet it from docx template into pdf
namespace App\Controllers\Services;
use App\Controllers\BaseController;
use Illuminate\Support\Facades\Response;
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\PhpWord;
class Contract extends BaseController
{
public function getContract()
{
$file = storage_path()."/contracts/contract.docx";
$tmpFile = storage_path()."/contracts/output.pdf";
$template = new TemplateProcessor($file);
$template->setValue('COMP_NAME',"LOREM IPSUM INC");
$template->setValue('ADDRESS',"Nowhere Str Tsastikistan");
$template->saveAs($tmpFile);
return Response::make("OK");
}
}
But the code fails to generate a readable pdf: https://drive.google.com/file/d/1wYsVrjwQNMN8r3sWZlOm6x7cNeja3ET0/view?usp=sharing
Ι also tried to generate an html so I can convert it into pdf: https://drive.google.com/file/d/1wYsVrjwQNMN8r3sWZlOm6x7cNeja3ET0/view?usp=sharing
But still the output seebs pretty much garbage.
Therefore do you have any idea how I can convert a docx template in PDF, if yes how I can use in-memory file instead of the filesystem?
Directly you cannot. You need to save it as docx first and thean read it again and save it as PDF:
namespace App\Controllers\Services;
use App\Controllers\BaseController;
use Illuminate\Support\Facades\Response;
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\IOFactory;
class Contract extends BaseController
{
public function getContract()
{
$file = storage_path()."/contracts/contract.docx";
$tmpFile = storage_path()."/contracts/output.pdf";
$outfile = storage_path()."/contracts/output.docx";
$template = new TemplateProcessor($file);
$template->setValue('COMP_NAME',"LOREM IPSUM INC");
$template->setValue('ADDRESS',"Nowhere Str Tsastikistan");
$template->saveAs($outfile);
$phpWord = IOFactory::load($outfile);
$phpWord->save($tmpFile,'PDF');
return Response::make("OK");
}
}
A solution I have not tested though is to use php://temp or php://memory in order to avoid filesystem IO.
I'm using PHPSpreadsheet to create Excel.
I want t generate the Excel file, then convert the Excel file in a PDF one.
So I've done the following :
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
use PhpOffice\PhpSpreadsheet\Reader\Exception;
class DevisGenerator
{
public function runDevis()
{
$spreadsheet = $this->loadexcelTemplate();
$uuid = $this->uniqidReal();
$filename = $this->writeName($spreadsheet, $uuid);
$this->convertPdf($spreadsheet, $filename);
}
public function writeName($spreadsheet, $uuid)
{
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->getCell('B2')->setValue('Toto');
try {
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$filename = $uuid;
$writer->save($filename.'.xlsx');
}catch (Exception $e)
{
//TODO gestion erreur
}
return $filename;
}
public function convertPdf($spreadsheet, $filename)
{
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
$writer->save($filename.'.pdf');
}
But whan I run the code the following error appear :
Attempted to load class "Mpdf" from namespace "Mpdf".
Did you forget a "use" statement for "PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf"?
I did not understand this error, I have correctly insert the use statement in my code.
Any idea ?
I've already got a similar issue with Mpdf.
PHPSpreadsheet supports multiple Librairies to generate PDF.
I'm nut using Mpdf but Tcpdf.
I'm also not sure but you need to install them manually.
composer require tecnickcom/tcpdf
Then in your code :
$writer = new Tcpdf($spreadsheet);
And don't forget the use statement ;)
Hope this help !
After use statement before class, you should use only:
public function convertPdf($spreadsheet, $filename)
{
$writer = new Mpdf($spreadsheet);
$writer->save($filename.'.pdf');
}
Since you use the fully qualified namespace when creating the instance, the use statement is not taken into account (thus the error message).
It seems like you have added a supplementary slash at the beginning of the namespace when creating your Mpdf instance, removing it will solve your issue.
$writer = new PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
But since you have added a use statement, you do not need to use the fully qualified namespace again, you can do
$writer = new Mpdf($spreadsheet);
I'm trying to convert a odt file to a pdf, using PHPWord and DomPDF, I've read some issue on their git about how to do it and found this one guide. But I'm getting and error on laravel about the PHPWord library Undefined index: document on the route
C:\xampp\htdocs\DAW2M14\vendor\phpoffice\phpword\src\PhpWord\Reader\Word2007.php
foreach ($steps as $step) {
$stepPart = $step['stepPart'];
$stepItems = $step['stepItems'];
foreach ($relationships[$stepPart] as $relItem) {
$relType = $relItem['type'];
if (isset($stepItems[$relType])) {
$partName = $stepItems[$relType];
$xmlFile = $relItem['target'];
$this->readPart($phpWord, $relationships, $partName, $docFile, $xmlFile);
}
}
}
The error starts in the foreach.
My PHP code is:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\URL_Document;
use App\Document;
use App\Http\Requests;
use Dompdf\Dompdf;
class CU_13Controller extends Controller
{
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function generaPDF(Request $request, $id, $nombre, $path, $pathb, $formato) {
$ruta=$path.'/'.$pathb;
if($formato == "pdf"){
return response()->download(storage_path("app/{$ruta}"));
}else{
$FilePath = "app/".$ruta;
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$document = $phpWord->loadTemplate(storage_path("app/{$ruta}"));
$document->saveAs('temp.odt');
$domPdfPath = base_path('/../vendor/dompdf/dompdf');
\PhpOffice\PhpWord\Settings::setPdfRendererPath($domPdfPath);
\PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF');
$phpWord = \PhpOffice\PhpWord\IOFactory::load('temp.odt');
$pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter( $phpWord, 'PDF' );
$pdfWriter->save(storage_path("app/documents{$nombre}.pdf"));
return response()->download(storage_path("app/documents{$nombre}.pdf"));
}
}
}
I don't know what kind of error I am doing
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();
}
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