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.
Related
I try to generate pdf from PHPJasper and this error come and i cannot find a solution for. Please tel me answer or any other way to generate reports
"Your report has an error and couldn 't be processed!\ Try to output
the command using the function output(); and run it manually in the
console."
<?php
require __DIR__ . '/vendor/autoload.php';
use PHPJasper\PHPJasper;
try{
$input = __DIR__ . '/Report/Coffee.jasper';
$output = __DIR__ . '/Report';
$options = ['format' => ['pdf']];
$jasper = new PHPJasper;
$jasper->process($input, $output, $options)->execute();
} catch(Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
Did you try the output() command as mentioned in the message?
You can try something easy as this and check the output of it:
$x = $jasper->process(
$input,
$output,
$options
)->output();
print_r($x);
exit(1);//just to avoid getting error handler on your framework
Sometimes that will tell you more information. If it shows the command line to execute, try executing it directly on your command line, you might get more information there.
Common things to consider:
- The user (probably the Web server user in your case) has no access or write access to the folder where you are creating the file
- Perhaps the line was already generated before, and the error is that the file already exists. Try generating a random name for each report generation to avoid this; or generate the file, serve it in the application and delete it
I want to convert a webpage to PDF in PHP using the library wkhtmltopdf. I'm using this PHP wrapper mikehaertl/phpwkhtmltopdf available here. I downloaded wkhtmltopdf binary from here and I used the below PHP code to point to it :
require __DIR__.'/vendor/autoload.php';
$pdfName = 'Invoice1.pdf';
$pdf = new mikehaertl\wkhtmlto\Pdf("some HTML code here");
$pdf->binary = __DIR__.'/wkhtmltopdf_0.12.4/bin/wkhtmltopdf';
if (!$pdf->send($pdfName, true)) {
echo $pdf->getError();
}
I'm getting this error: sh: 1: /var/www/mysite/public_html/wkhtmltopdf_0.12.4/bin/wkhtmltopdf: not found
I don't understand why it is saying file not found. I tried to check with PHP if the file exists using file_exists and it returns true when I provide to it that location.
Any help please?
Thanks.
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.
I am using pdfparser for copy text from PDF files but some PDF files are copy protected or have different fonts so that pdfparser not working for that, is it possible to get text from copy protected PDF?
This is my Code :
// Include Composer autoloader if not already done.
error_reporting(E_ALL);
ini_set('display_errors', 1);
include 'vendor/autoload.php';
// Parse pdf file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile('tests.pdf');
// Retrieve all pages from the pdf file.
$pages = $pdf->getPages();
// Loop over each page to extract text.
foreach ($pages as $page) {
echo utf8_encode($page->getText());
}
?>
After trying this code I am not getting any error or warning. This code is only showing blank space. I have also try utf-8 encoding but still it is not working?
If the author of the PDF specified the Permissions flags of the document to not permit Copying or Extracting Text and Graphics then you should consider that. Not all PDF software respects such restrictions however.
\Smalot\PdfParser can't extract password protected files.
I've found a far better solution for that (providing your PHP service is running on a Linux server): use the command line tool “pdftotext” (included in the “poppler” package in, for example, Debian or Ubuntu).
It perfectly handles password protected files (it has an option to give password if required).
Used with something like this, inside a PHP script under web server on a Linux server, with a PDF file submitted through a web form:
// $filepath is the full file path properly extracted from the $_FILES variable
// after form submission.
// Expected running under Linux+Apache+PHP; if not, you may have to find your way.
if (! file_exists($filepath)) {
// In case systemd private temporary directory feature is active.
$filepath = '/proc/'.posix_getppid().'/root'.$filepath;
}
$cwdt = 4; // may be better fine tuned for better column alignment
// “sudo” is necessary mostly with systemd private temporary directory
// feature. Needs proper sudoers configuration, of course.
$cmd = "sudo /usr/bin/pdftotext -nopgbrk -fixed {$cwdt} {$filepath} -";
exec($cmd, $output, $res);
print_r($output);
I don't know if it is an answer to the “or having different fonts” requirement, however.
I am running in a Windows10 environment. My php script writes a pdf file and I simply want to have the file open up in the pdf viewer that I specify. I am using the "runAsynchronously" function given in the PHP manual and I have tried many variations. I have no problem getting the process to run in the background - it appears every time in my TaskManager process listing, but no window appears - what am I doing wrong? If I double click the link file that has been written it works fine. It is nothing to do with the path to the executable or the filename - I can replace the pdf viewer with "notepad.exe" and the $file with a suitable text file - the same thing happens, notepad appears as a process, but not as a window, and the link works fine.
Here are some code snippets
$cmd = "C:\\Program Files (x86)\\SumatraPDF\\SumatraPDF.exe";
runAsynchronously($cmd, $file, 7, null, true);
function runAsynchronously($path, $arguments, $windowstyle=1, $lnkfile=null, $exec=true) {
$tmp = (is_null($lnkfile)) ? 'C:\temp\temp.lnk' : $lnkfile;
try {
if(file_exists($tmp)) { unlink($tmp); }
$WshShell = new COM("WScript.Shell");
$oShellLink = $WshShell->CreateShortcut($tmp);
$oShellLink->TargetPath = $path;
$oShellLink->Arguments = $arguments;
$oShellLink->WorkingDirectory = dirname($path);
$oShellLink->WindowStyle = 1;
$oShellLink->Save();
$waitforcompletion = false;
if($exec) {
// Run kicks off the process in the background, but no window gets opened
$oExec = $WshShell->Run($tmp, $windowstyle, $waitforcompletion);
unlink($tmp);
} // if not executed link is left available for manual running
unset($WshShell,$oShellLink,$oExec);
} catch(Exception $ex) {
print $ex->getMessage();
}
}
If opening in the browser a pdf or downloading it solves your problem, try a class called PDFMerger:
https://github.com/clegginabox/pdf-merger
It uses an API called setasign FPDI and FPDF (https://packagist.org/packages/setasign/fpdi-fpdf#p-456)
I tested it here, because it also interested me and worked ok. (Wamp in Windows 8.1)
I put everything together(classes and folders of fpdi and fpdf) in a folder called pdfmerger because I was having some difficulty with namespaces and my code worked ok, but I know this is not the best way to include the classes - the example in GitHub page must help if you have problems with classes not finding classes):
<?php
include "pdfmerger/fpdf.php";
include "pdfmerger/fpdi.php";
include 'pdfmerger/PDFMerger.php';
$pdf = new PDFMerger();
//generate a pdf with the pages 1,2 and 3 and send to browser
$pdf->addPDF('originalpdfsavedintheserver.pdf', '1,2,3')
->merge('browser', 'nameinbrowser.pdf');
// REPLACE 'browser' WITH 'file', 'download', 'string', or 'browser' for output options
If you use the option 'download' you can choose the program that will open the pdf file in your windows settings(Choose default program to open pdfs):
https://www.cnet.com/how-to/how-to-set-default-programs-in-windows-10/