TCPDF - How to convert a HTML page into PDF - php

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

Related

mPDF: Generate PDF Exactly Like Print Preview

I am using mPDF through PHP in order to generate an exact replica PDF of an HTML page. This PDF is then being saved to the server so that it can be automatically printed for the user through PHP socket programming.
When I print preview the HTML page, it looks exactly as I would want it printed. When I convert the HTML to a PDF with mPDF, it becomes wide and distorted. I just want it to look exactly like the HTML print preview with no changes.
Here is my PHP code:
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
ob_start();
include 'cert.html';
$html = ob_get_contents();
ob_end_clean();
$mpdf->WriteHTML($html);
$mpdf->Output();
Ditch mPDF and use a browser based library such as wkhtmltopdf.

PDF cretaion of webpage with fpdf return blank

I am trying to create direct pdf of webpage. so i am using fpdf for that.
I have kept fpdf files on fpdf folder and created a separate create-pdf.php file with following code :
<?php
require('fpdf.php');
$url = $_GET[$url1];
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,$url);
$pdf->Output();
?>
And used following php code to get current url of page of which pdf is to be created.
<?php
$url1 = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
?>
And Created a Link to create pdf as follows :
Create PDF
Now pdf is getting created in new tab...but It is Empty / blank...No text is there on created pdf...

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

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.

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.

how to generate pdf and save to server

Well, i'm trying to generate pdf file and save it to server but when i'm trying to view that pdf its showing invalid format.
Code :
<?php
$content = "32w434";
file_put_contents("xyz.pdf",$content);
?>
If i change file from pdf to doc it works and opening perfectly but not pdf
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$content = $pdf->Output();
file_put_contents("xyz.pdf",$content);
That code giving output i don't want output on browser and still file is corrupted / invalid format
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$content = $pdf->Output('doc.pdf','F');
http://www.fpdf.org/
You can use FPDF to generate pdf file....
http://www.fpdf.org/
This is simple and best way to generate pdf...
Use TCPDF to generate PDF file,
TCPDF Site: http://www.tcpdf.org/
Demo: http://www.tcpdf.org/examples.php
#Mohit,
This
$content = $pdf->Output('doc.pdf','F');
generates a new file. If your file is generated using variables that can change in php, the pdf file you see on screen, will be different from the pdf file saved on the server.
You should generate pdf file using pdf libraries such as tcpdf or fpdf. Putting contents directly into a file with .pdf extension will definitely not working since pdf has its own file format

Categories