I have downloaded the updated version of mpdf and using it with php. It is giving me the following error.
"fatal error: Trait 'Mpdf\Strict' not found in
E:\xampp\htdocs\PDF\mpdf\Mpdf.php on line 39".
$html = '<h2>mpdf test.</h2>';
include("mpdf/mpdf.php");
$mpdf = new mPDF('c','A4','','',32,25,27,25,16,13);
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first
level of a list
// LOAD a stylesheet
$stylesheet = file_get_contents('mpdfstyletables.css');
$mpdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is css/style only and no body/html/text
$mpdf->WriteHTML($html,2);
$mpdf->Output('mpdf.pdf','I');
exit;
You are directly including mpdf via
include("mpdf/mpdf.php");
which will only include the core file but not any other file mPDF may need in the generation process. The correct way would be to use:
// Require composer autoload
require_once __DIR__ . '/vendor/autoload.php';
// Create an instance of the class:
$mpdf = new \Mpdf\Mpdf();
This will make sure that required classes will be autoloaded as soon as they are referenced.
For more info, check the "Getting started" chapter on the mPDF web page.
Related
I am trying to password protect PDF documents with a PHP script. So I downloaded and included the FPDI_Protection.php file in my script which contains the FPDI_Protection class.
But when I'm trying to execute the script it says the Class FPDI_Protection not found when its clearly present in the mentioned file. I also checked other answers on stackoverflow which says to use setasign\ but no such namespace is declared in file containing the class.
Also I'm not using composer for dependencies.
My Code goes like
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
function pdfEncrypt ($origFile, $password, $destFile){
require_once("assets/fpdi/FPDI_Protection.php");
$pdf =& new FPDI_Protection();
// set the format of the destinaton file
$pdf->FPDF("P", "in", array('8.27','11.69'));
//calculate the number of pages from the original document
$pagecount = $pdf->setSourceFile($origFile);
// copy all pages from the old unprotected pdf in the new one
for ($loop = 1; $loop <= $pagecount; $loop++) {
$tplidx = $pdf->importPage($loop);
$pdf->addPage();
$pdf->useTemplate($tplidx);
}
//password for the pdf file
$password = "test";
//name of the original file
$origFile = "cv.pdf";
//name of the destination file
$destFile ="cv_test.pdf";
//encrypt the book and create the protected file
pdfEncrypt($origFile, $password, $destFile );
?>
I have my FPDI_Protection.php file under assets folder of current directory of the script.
And I have downloaded the script from here.
In case that link doesn't work see the whole article here.
Tried all possible way to fix but none of them work. Can you please point out the mistake I am doing ?
You should not use a script from a blog post which is nearly 14 years old! Just use the official and up to date version of the FPDI Protection class which is available here.
I'm using mpdf on wordpress for generating PDF files. I'm working on a functionality which will send weekly report to my users and that report should be send as email and pdf will be attached in email.
My issue is that I'm running code in functions.php file because for running this code every week, I'm going to use server side cron job and my function should be inside functions.php file to execute it. So I added this code in functions.php file:
function weeklyReportFunc(){
include('mpdf/mpdf.php');
$mpdf = new mPDF();
ob_start();
require get_template_directory() . '/includes/report.php';
$x = ob_get_contents();
ob_end_clean();
$mpdf->WriteHTML($x);
$today = date('Y-m-d');
$pdfName = 'weekly-report-'.$today;
$mpdf->Output($pdfName.'.pdf', 'D');
}
And this shows me below error:
Warning: Cannot modify header information - headers already sent by (output started at
/home/user/public_html/doms/wp-admin/includes/template.php:1995) in /home/user/public_html/
doms/wp-content/themes/mytheme/mpdf/mpdf.php on line 8314
Warning: Cannot modify header information - headers already sent by (output started at
/home/user/public_html/doms/wp-admin/includes/template.php:1995) in
/home/user/public_html/doms/wp-content/themes/mytheme/mpdf/mpdf.php on line 1706
mPDF error: Some data has already been output to browser, can't send PDF file
How can I solve this? Maybe I need to use my function in some action? but which one? Any ideas please?
Finally found solution. So I created a php file in my theme folder and at the very top of the file added require('../../../wp-load.php'); code which makes all wordpress functions available inside it, even if this file is not a wordpress template page. So as now all functions are available inside this file, I moved my code from functions.php file to this file and I'm already running cron job on this file. Hope this will help someone else.
Save report.php output buffer to $x using exec():
function weeklyReportFunc(){
ob_start();
include('mpdf/mpdf.php');
$mpdf = new mPDF();
exec('php -f '.get_template_directory().'/includes/report.php',$output);
$x = $output[0];
$mpdf->WriteHTML($x);
$today = date('Y-m-d');
$pdfName = 'weekly-report-'.$today;
$mpdf->Output($pdfName.'.pdf', 'D');
}
I am trying to use Pdfparser library to parse a PDF file but I have some issues with classes inclusion.
I read the documentation but it doesn't works.
I use Windows and XAMPP.
I created a directory in /xampp/htdocs/pdf_import
I installed Composer and I've generated the /vendor/autoload.php in pdfparser-master/src
I use the code example in documentation
Example:
<?php
require 'vendor/autoload.php';
// Parse pdf file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile('document.pdf');
// Retrieve all pages from the pdf file.
$pages = $pdf->getPages();
// Loop over each page to extract text.
foreach ($pages as $page) {
echo $page->getText();
}
When I run the php script I obtain this error:
Fatal error: Class 'Smalot\PdfParser\Parser' not found in C:\xampp\htdocs\pdf_import\pdfparser-master\src\import.php on line 8
Somehow your path is not good for
require 'vendor/autoload.php';
Verify if autoload is actually included.
In Codeigniter3/4 be sure you put the path in your config file
$config['composer_autoload'] = 'vendor/autoload.php';
then in your controller/library
// Parse pdf file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile(FCPATH . 'includes/temp/' . $pdf_file);
return $pdf->getText();
I've generated a beautiful HTML invoice with MPDF, but when I placed it on the server it isn't showing anything.
The logs show the following for local (here the generation works):
http://pastebin.com/n3xJujBH
The logs show the following when generating on the server (here it shows an empty HTML page on generation, not PDF):
http://pastebin.com/HDeSPHse
The following code is used to generate the PDF in Codeigniter:
private function _gen_pdf($html,$paper='A4')
{
$this->load->library('mpdf53/mpdf');
$mpdf=new mPDF('utf-8',$paper);
$mpdf->debug = true;
$mpdf->WriteHTML($html);
$mpdf->Output();
}
The HTML created is following: http://pastebin.com/b3hFNbT8
Something to note is that, if I put only "test" in $html, it won't generate either.
Any ideas?
This got me pass the obstacle
$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8', 'tempDir' => __DIR__ . '/custom/tmp']);
Create custom (this can be anything) directory if it tells some permission error on the live server like on aws ubuntu instance etc...
The answer for me was switching to DomPDF. I never got in functioning on the external parallels server and will probably never know why..
I was facing the same issue.
I tried this code and it gave me the issue due to which mpdf was not working.
<?php
// Require composer autoload
require_once __DIR__ . '/vendor/autoload.php';
try {
$mpdf = new \Mpdf\Mpdf();
$mpdf->debug = true;
$mpdf->WriteHTML("Hello World");
$mpdf->Output();
} catch (\Mpdf\MpdfException $e) { // Note: safer fully qualified exception
// name used for catch
// Process the exception, log, print etc.
echo $e->getMessage();
}
?>
In my case i had to make the folder where i had kept my files writable.
I have started experimenting with codeigniter and pdfs. I'm using the latest version of both. For some reason, i'm getting this error when trying to render the pdfs:
Warning: require_once(C:\Users\Manfred\Dropbox\Web\Alodu\application\helpers\dompdf/include/ci_exceptions.cls.php) [function.require-once]: failed to open stream: No such file or directory in C:\Users\Manfred\Dropbox\Web\Alodu\application\helpers\dompdf\dompdf_config.inc.php on line 208
Fatal error: require_once() [function.require]: Failed opening required 'C:\Users\Manfred\Dropbox\Web\Alodu\application\helpers\dompdf/include/ci_exceptions.cls.php' (include_path='.;C:\php\pear') in C:\Users\Manfred\Dropbox\Web\Alodu\application\helpers\dompdf\dompdf_config.inc.php on line 208
Code used is:
function pdf()
{
$this->load->helper(array('dompdf', 'file'));
// page info here, db calls, etc.
/*
$data=array(
"$title"=>"Hello!",
"$test_questions"=>"1,2,3,4",
);
*/
$data['test_questions']= "hello";
$html = $this->load->view('pdf/test_ibdp', $data, true);
$filename="Test".$data['test_questions'];
pdf_create($html, $filename);
write_file('name', $data);
}
And the helper:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename, $stream=TRUE)
{
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper("a4", "portrait" );
$dompdf->render();
$dompdf->stream($filename . ".pdf");
}
?>
and the view (pure HTML)
<html>
<head>
<title>Hello!</title>
</head>
<body>
<h1>HelloAgain</h1>
</body>
</html>
Any suggestions? I'm not that experienced in PHP and i'm quite confused. I just re-downloaded the library, i've tried keeping it really simple by stripping away extras in my code. Nothing seems to work. any help would be great :)
This is a class autoloader issue. Which version of DOMPDF do you use? I think dompdf 0.5 had a problem when integrated inside a framework like CI.
The 0.6 version doesn't have this probleme anymore, and if the problem persists, write
define("DOMPDF_AUTOLOAD_PREPEND", true)
in dompdf_config.custom.inc.php.
The error is saying the path below is not correct. Recheck the path
require_once("dompdf/dompdf_config.inc.php");
Path to the file is incorrect.
C:\Users\Manfred\Dropbox\Web\Alodu\application\helpers\dompdf/include/ci_exceptions.cls.php
You can use DIRECTORY_SEPARATOR.
Correct the dompdf file path. Also remove this line of code:
write_file('name', $data);
The following line is enough:
pdf_create($html, $filename);