When I am trying to upload a pdf file and separating each page as pdf, with some pdf files it is working but some of pdf files show this error:
mPDF error: Unable to find xref table -" Maybe a Problem with auto_detect_line_endings"
My code:
ini_set('memory_limit', '512M');
$pagecount = Model::count_pages($documentPath.$journalDoc);
for ($i=1; $i<=$pagecount; $i++) {
$pdf = new mPDF('','Letter',12,'helvetica, sans-serif',200,0,0,20,0,10,'P');
$pdf->SetImportUse();
$pdf->SetSourceFile($documentPath.$journalDoc);
$import_page = $pdf->ImportPage($i);
$pdf->UseTemplate($import_page);
$pdf->Output($output_dir.$i.'.pdf', 'F');
}
This can be caused by PDF files of a version incompatible with the mPDF you're running. You can often circumvent the problem by regressing the source PDF files.
For instance, for mPDF v6.0 try brining your PDFs down to at most v1.4 using something like Ghostscript. (Where old.pdf is you're sourcefile)
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -o new.pdf old.pdf
Ghostscript won't write to the file it's reading so if you're doing this inline you'll have to dance;
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -o new.pdf old.pdf; mv -f new.pdf old.pdf
Increase the length size of this
public static $trailerSearchLength = 5500;
to this
public static $trailerSearchLength = 500000;
file location is: vendor\setasign\fpdi\src\PdfParser\CrossReference\CrossReference.php
This can be caused by the PDF file version that is incompatible with mPDF. You would better regress the PDF file version.
If you extracted a page from a file, you may have used an Adobe software.
Just use pdftk or pdfchain on Linux platform to solve the problem.
The version of FPDI in the offical repo of mPDF is very old. You may try to update to the latest version. Or you simply use the official version of FPDI which uses FPDF.
If you get a message about "unssported compression" you may check out the FPDI PDF-Parser add-on. Notice that there's a license incompatibility to mPDF.
Related
I'm playing with Mpdf for generating some html as a PDF but HTML tags don't seem to be working
I'm using Mpdf 7.1.6 and PHP 7.2
I can include stylesheets or not with no difference
$pdf = new \Mpdf\Mpdf(["format" => "A4"]);
$pdf->WriteHTML('<h1>Heading 1</h1>');
$pdf->WriteHTML('<h2>Heading 2</h2>');
$pdf->WriteHTML('<h3>Heading 3</h3>');
$pdf->WriteHTML('<h4>Heading 4</h4>');
$pdf->WriteHTML('<b>Bold</b>');
$pdf->WriteHTML('<center>Center</center>');
$pdf->WriteHTML('<table><tr><td>Table</td><td>Table</td></tr><tr><td>Table</td><td>Table</td></tr></table>');
$pdf->Output();
and I get the following
Not sure what the issue is.
Had to install Mpdf manually as I'm on a shared server and don't have command access nor access to composer so it could be a missing library of config file.
Any suggestions would be greatly appreciated as it's doing my head in.
Please I am trying to use snappy for the first time to allow the user download PDF from my site. I previously used DomPDF but found it not suitable for the current situation. I first installed wkhtmltopdf, then I installed snappy using composer which installed it to c/users/computer-name/vendor (I am very new to composer). I copied the vendor folder to my project directory.
I added the code below;
require __DIR__ . '/vendor/autoload.php';
use Knp\Snappy\Pdf;
$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
echo $snappy->getOutput('http://www.github.com');
as shown here: https://github.com/knplabs/snappy
The PDF downloads but does not open. Chrome says "Failed to load PDF document". When I open the PDF with notepad, I see the error;
Fatal error: Uncaught RuntimeException: The exit status code '1' says something went wrong:
stderr: "The system cannot find the path specified.
"
stdout: ""
command: /usr/local/bin/wkhtmltopdf --lowquality "https://www.google.com/" "C:\Users\CHIDIE~1\AppData\Local\Temp\knp_snappy5e42659b757116.59025588.pdf". in C:\xampp\htdocs\corporatecareer\templates\vendor\knplabs\knp-snappy\src\Knp\Snappy\AbstractGenerator.php:381
Please help. What are my missing. I know I am doing something wrong.
Thanks.
/usr/local/bin/wkhtmltopdf in snappy example is a linux binary location. Seems like you are using windows xampp. Download the wkhtmltopdf program from here https://wkhtmltopdf.org/downloads.html and update you path where you extracted the binary like so.
$snappy = new Pdf('C:\path to where you extracted binary');
Also, make sure that the extracted path doesn't required admin privileges to execute.
Below is the error that I am getting while I use the code
$output = shell_exec("/usr/bin/ebook-convert test.epub mech4eck.pdf");
<br>echo $output;
I need to run this with PHP only, and so I am trying to execute the shell commands. I am using Ubuntu 12.
No write acces to /root/.config/calibre using a temporary dir instead /opt/lampp/lib/libgcc_s.so.1: version GCC_4.2.0' not found (required by /usr/lib/i386-linux-gnu/libstdc++.so.6) /opt/lampp/lib/libgcc_s.so.1: versionGCC_4.2.0' not found (required by /usr/lib/i386-linux-gnu/libstdc++.so.6) 1% Converting input to HTML... InputFormatPlugin: EPUB Input running on /opt/lampp/htdocs/test.epub Found HTML cover content/calibre_title_page.html Parsing all content... 34% Running transforms on ebook... Merging user specified metadata... Detecting structure... Flattening CSS and remapping font sizes... Source base font size is 12.00000pt Removing fake margins... Cleaning up manifest... Trimming unused files from manifest... Creating PDF Output... 67% Creating PDF Output
I know none out here helped but I figured out on it my own.
Here is the trick
Rename the file /opt/lampp/lib/libgcc_s.so.1 to /opt/lampp/lib/libgcc_s.so.1.bak
Taddda it works :)
I am running the following code and giving me this error : FPDF error: This document (testcopy.pdf) probably uses a compression technique which is not supported by the free parser shipped with FPDI. I used another pdf named test.pdf and that works fine but it is giving me error in testcopy.pdf.
I think this is parser problem. Anyone know any other parser that can be used with fpdf to avoid this error?
My code:
require('fpdf17/fpdf.php');
require('fpdf17/fpdi.php');
// initiate FPDI
$pdf = new FPDI();
while (ob_get_level())
ob_end_clean();
header("Content-Encoding: None", true);
// set the sourcefile
$pagecount = $pdf->setSourceFile('testcopy.pdf');
I want to split pdf in two pdfs and want to attach both pdfs in file attachments field.How to save pdf to server. Can it be possible with fpdf?
Yes, Exactly the FPDF library supports only PDF version 1.4 & lesser. So to over come from this issue use GhostScript. This script helps to change the PDF versions dynamically.
(1) Download the Ghostscript here.
(2) Install the Ghostscript and define the environment variable PATH.
(3) Then, use the below php code to change the PDF version.
shell_exec( "gswin32 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=".$new_pdf." ".$old_pdf."");
(4) Now we get PDF with version 1.4 as output, then continue with FPDF.
Enjoy!
I had the same issue that PDFMerger does not work with certain PDF.
It seems that Adobe keep updating compression way of pdf extension.
I found solution with Chrome.
1. Open the PDF file(which does not work with PDFMerger) with Chrome
2. Right Click and Print
3. Change destination as [Save as PDF]
4. This exported PDF file is compatible with PDFMerger
The file needs to be PDF 1.4 (Acrobat 5) or lower. If you have Adobe Acrobat Pro you can change this with Document->Decrease File Size.
Looking through the answers so far, I wasn't able to resolve the issue.
Here's why and you may have the same issue so this answer would be invaluable to you too.
I did not have access to acrobat, so I couldn't downgrade the version of the original PDF. I wasn't in a position to purchase the newer version of the library which has upgraded encryption.
In the end I used an online PDF converter to downgrade my PDF file to version 5 (1.4)
If you are not able to find one, here's a link for completeness.
https://docupub.com/pdfconvert/
(bear in mind that the contents of a link may change)
Here is my answer from another topic:
"It can be overcome by re-saving the template and change compression type.
In Adobe Acrobat Pro go to File->Save As->Optimised PDF-> Popup left list - Clean Up,
Object Compression Options -> Change to "Compress document structure" -> OK to save"
Ref. Editing password protected pdfs with fpdi
GPL Ghostscript 8.70 (2009-07-31) / Centos 7
ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=new_file.pdf file.pdf
For those that don't have any of the pro versions of Acrobat:
I was having this problem with new versions of Word when trying to use the Office functionality to save to a PDF. It seems the PDF conversion tool supplied in Office is using the newer formats. Here's how I fixed it.
Install NitroPDF free version (May work with another free PDF creator that can be downloaded)
Go to the Word document and select 'Print'.
In the print options, instead of your printer, select 'Nitro PDF Creator (Reader 3)' or whatever the current version is.
A dialog box will open and allow you to select where to save the PDF.
The version saved was able to be opened by FPDF.
Hope it helps
In the case of Mac, you can open the PDF file in Preview and "export as PDF". The exported one can then be used.
I had a very similar error like you. My solution was to convert the pdf to a lower pdf version. Than everything worked like expected.
I hope that simple trick will help you!
some of the fonts like ArialMT, Arial-boldMT, PTSans-bold..
when i am using this font like
$pdf->SetFont("ArialMT", "", 22);
then i got error like..
TCPDF ERROR: Could not include font definition file
when i saw the font folder of TCPDF library then i could not see this type of font..
how to install this font in TCPDF to solve the error and get effect of this font??
please help it is urgent..
thank you in advance..
i found one solution that in "fonts/utils/ttf2ufm.exe" then run following to command 1)$ ttf2ufm -a -F myfont.ttf and 2)$ php -q makefont.php myfont.ttf myfont.ufm first command is successfully executed but 2nd is not executing in windows command promt so tell me what i have to do for php file as you specify in "comici.php"..
Try this
$pdf->AddFont('Comic','I');
// is equivalent to:
$pdf->AddFont('Comic','I','comici.php');
You will find tutorial here.
http://api.joomla.org/com-tecnick-tcpdf/TCPDF.html#AddFont
Have you tried adding the font first?
$pdf->AddFont("ArialMT", "", 22);
If that does not work you can include the font in the library by using the command
$fontname = TCPDF_FONTS::addTTFfont(FCPATH.'/assets/css/fonts/ArialMT.otf');
For that you need to have the "otf" or "ttf" version of the font and set the correct directory.
For example I am using:
$fontname = TCPDF_FONTS::addTTFfont(FCPATH.'/assets/css/fonts/arialunicode050418/ArialUnicodeMS.otf');
// This should be ran only once to include the font. You can comment it afterwards.
$pdf->AddFont('ariaunicodems', '', 10, '',false);
$pdf->SetFont('ariaunicodems', '', 10, '',false);
You can print the "fontname" variable to see the correct name of the font to be added or set. It does not respond to the name of the font file.
TCPDF have a tool(tcpdf_addfont.php) to install any new font which is located in 'your_path_to_tcpdf/tools/ '
You can use the following command to install a new font to TCPDF.
path_to_tcpdf/tcpdf/tools/tcpdf_addfont.php -i path_to_ttf/ArialMT.ttf
The code above is pretty much self explanatory.
You can download the True Type (ttf) for any font easily using Google Search.
After running the above command you will get a output similar to:
>>> Converting fonts for TCPDF:
*** Output dir set to /path_to_tcpdf/tecnickcom/tcpdf/fonts/
+++ OK : path_to_ttf/ArialMT.ttf added as arial
>>> Process successfully completed!
Now use this font in TCPDF like any other font:
$pdf->SetFont("arial", "", 22);
You only have to create the font file once with TCPDF. This can be done, for example, with a separate PDF script.
It is important to know the exact path of the original ttf and, if necessary, to place it in the appropriate folder on the server.
TCPDF_FONTS::addTTFfont('File path to the ttf file.', 'TrueTypeUnicode', '', 32);
For all commands and details see:
https://stackoverflow.com/a/70337995/2320007