PDF generated by TcPDF (PHP) won't open in Adobe Acrobat XI - php

I'm able to generate a PDF file using TcPDF ver 6.0.44. The file opens fine in the browser, and 3rd party PDF readers but will not open in Adobe Reader XI. I keep getting a the error message 'file type is not supported or the file is damaged'.
What's going on here? Here's the code I wrote:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->AddPage();
$html = '<html>
<head></head>
<body>
<h1>Hello World!</h1>
</body>
</html>';
$pdf->lastPage();
$pdf->writeHTML($html, true, 0, true, 0);
$pdf->Output('htmlout.pdf', 'I');

In your test file if you are using images , and some of your images are missing icc profile, adobe acrobat doesnt recognize this and cause the problem you are facing.

Related

TCPDF - How to convert a HTML page into PDF

I have a page and I just want it to save it as pdf.
Like if you click print on the webpage and save as pdf or microsoft print to pdf.
Here is my code
`<?php
// Include the main TCPDF library (search for installation path).
require_once('./TCPDF-main/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->setTitle('Title');
$pdf->AddPage();
//To print
$html = 'https://examplewebsite.com';
//Write to pdf
$pdf->writeHTML($html);
$pdf->Output('Newp file.pdf');
?>`
Currently it is creating a pdf which has https://examplewebsite.com but not the contents. How to print the contents?
The $html variable is a string not a html source, you need to load the url with file_get_contents $html = file_get_contents('https://examplewebsite.com') or a curl method: php: Get html source code with cURL

Weird char in generating PDF from CKEditor

I have issue with PDF generated from CKEditor.
I have some text typed in CKEditor:
HTML source:
Next, this text is saved in DB.
My CKEditor config looks like:
Further, above text is fetched from DB in controller, and PDF file is generated using TCPDF:
public function createRegulationPDF(){
require_once('TCPDF/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set font
$pdf->SetFont('freeserif', '', 10);
// add a page
$pdf->AddPage();
// GET CONTENT FROM DB
$html = $this->view->settings['reg_statement'];
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
....
And the final point, generated PDF:
The question is: what is this weird (the box) selected char?
And how to avoid it?

SVG to PDF conversion using TCPDF in PHP

I am trying to convert a SVG file to PDF file using TCPDF library in PHP. I have created a SVG file and use PHP to replace text and plan to render the resultant SVG file to PDF file.
Any idea, if TCPDF library supports SVG to PDF conversion. Any pointers in this direction would really help me.
You don't really need to replace text or render, Once you have created your svg file. All you just need to include tcpdf in your script and create its object like e.g.
require_once(DOCUMENT_ROOT . '/library/Lib/tcpdf/mypdf.php');
$this->pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$this->pdf->AddPage(); // Add page to pdf before addding content
//There are several other property need to be set on basis of your need
$this->pdf->ImageSVG('file/mySVGFile.svg', 15, 20, '', '', '',
'', '', 1, false); // 15,20 are co-ordinate to position graph in pdf
Once you added your content to your pdf, Last step comes is to download the pdf using .
$this->pdf->Output('my.pdf', 'FD');
Feel free to ask for anything you have any query in this code.
Try
$pdf->ImageSVG(VIEW_JS."graph_as_svg/".php", '', '', '210', '100', '', '', '', 0, false);

AddPage in tcpdf not working

I have html, when I run that html it is working well. But, when I want to create pdf of that html, design changes and I am not getting what design I have.
Also I can not add pages, my design has two page and I want the pdf to be in two pages.
here is my sample code:
require_once('eng.php');
require_once('tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('RoyalHome.ae');
$pdf->SetTitle('Listing ');
$pdf->SetSubject('PDF of Listings');
$pdf->SetKeywords('Royalhome, PDF, listing');
$pdf->SetFont('Helvetica', 'B', 10);
// add a page
$pdf->AddPage();
my html is here fiddle
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->SetFillColor(255,255,0);
$pdf->lastPage();
$sr = $fileName . '.pdf';
$pdf->Output($sr, 'D');
Just tested your code. Ran into the following issues:
You have relative URLs for img tags, which, on my machine, results in tcpdf throwing an error when trying to fetch the images. Make sure you use absolute URLs to avoid this.
You are using the D flag for Output destination. I got errors because headers were already sent. If you are generating and outputting the pdf after the page loads, or if errors are thrown and output to screen, the output will fail.
I removed all <img /> elements and changed the destination flag to F and the pdf generated just fine on my desktop.

Creating PDF with tcpdf shows blank on iphone

I am using tcpdf to produce PDF's from HTML. Everything is working fine and when I view the PDF on my computer I can see it just fine, but for some reason when I look at the PDF on my iPhone it shows up blank. All I can see are the borders I created that contain values in them but there are no values showing.
Here is my code
require_once('/tcpdf/tcpdf/config/lang/eng.php');
require_once('/tcpdf/tcpdf/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setLanguageArray($l);
$pdf->SetFont('dejavusans', '', 10);
$pdf->AddPage();
$html = file_get_contents('http://www.website.com/invoice.php?invoice_id='.$invoice_id);
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();
$pdf->Output('/html/admin/emailattachements/invoice.pdf', 'F');
In that last line. I copy the PDF to that directory when I grab it later with an email script and sends it off to a customer.
Edit: SOLVED
I discovered it was the font I was using to produce the PDF. iPhone's can't read dejavusans :) I changed it to 'times' and it works fine
Edit: Update
Since this article I have had to create many more PDF's with tcpdf and while I can't really explain why some fonts were not working while others where I recently applied some of the suggestions over at http://www.tcpdf.org/fonts.php and applied
$fontname = $pdf->addTTFfont('/path-to-font/DejaVuSans.ttf', 'TrueTypeUnicode', '', 32);
By adding a font manually and setting the font file path and uploading the file manually I was able to get existing font's that did not work and actually get them to work.
Change the below lines:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetFont('dejavusans', '', 10);
To:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);
$pdf->SetFont('helvetica', '', 10, '', true);
And then check.
Not sure how its working for you without the AddFont method, but I had the same issue and adding the fourth param $subset = false fixes it.
$subset (mixed) if true embedd only a subset of the font (stores only
the information related to the used characters); if false embedd full
font; if 'default' uses the default value set using
setFontSubsetting(). This option is valid only for TrueTypeUnicode
fonts. If you want to enable users to change the document, set this
parameter to false. If you subset the font, the person who receives
your PDF would need to have your same font in order to make changes to
your PDF. The file size of the PDF would also be smaller because you
are embedding only part of a font.
$pdf->AddFont('dejavusans', '', 'dejavusans', false);
$pdf->AddFont('dejavusans', 'B', 'dejavusans', false);
$pdf->SetFont('dejavusans', '', 10);
dejavusans fixes a number of issues with multibyte characters so not possible to simple change to helvetica. By making this change we found the PDF to more than double in size.
This is kinda the same idea as #user3548394 but only have to make this change once.
Check the font type you are using, is like html, but it won't fallback.
Create pdf with static text and check.

Categories