php to pdf using dompdf - php

I have following codes for start.
<?
require('html2fpdf.php');
$pdf=new HTML2FPDF();
$pdf->AddPage();
$fp = fopen("en/print-job.php?ID=12","r");
$strContent = fread($fp, filesize("en/print-job.php?ID=12"));
fclose($fp);
$pdf->WriteHTML($strContent);
$pdf->Output("home.pdf");
echo "PDF file is generated successfully!";
?>
I have a page called print-pdf.php which is built on bootstrap & output is something like this:
https://www.lotomanager.in/en/print-job.php?ID=12
so how can this page be converted as it is to pdf?
I get following result from above codes:
Warning: fopen(en/print-job.php?ID=12): failed to open stream: No such
file or directory in /home/loto/public_html/pdf.php on line 5
Warning: filesize(): stat failed for en/print-job.php?ID=12 in /home/loto/public_html/pdf.php on line 6
Warning: fread() expects parameter 1 to be resource, boolean given in /home/loto/public_html/pdf.php on line 6
Warning: fclose() expects parameter 1 to be resource, boolean given in /home/loto/public_html/pdf.php on line 7
PDF file is generated successfully!

Try this
<?php
require_once 'dompdf/autoload.inc.php';
?>
<?php
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$html = file_get_contents('http://www.lotomanager.in/en/print-job.php?ID=12');
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
?>

Not sure what you mean. If you meant to display the ID in the document:
$dompdf->loadHtml('hello world ' . $_GET['ID'] );

DOMPDF has a load_html_file method that you can use. I have also cast the $_GET as an integer for security.
<?php
require_once 'dompdf/autoload.inc.php';
?>
<?php
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->load_html_file('https://www.lotomanager.in/en/print-job.php?ID='.(int)$_GET['ID']);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
?>

You do this by refactoring print-job.php in a way that you get some function generating the HTML which can either print the HTML to the user or feed it into the PDF library.
The most simple way might be
ob_start();
include('print-job.php');
$html = ob_end_clean();
But this can lead to trouble with different global variables or something in both files and you have to be careful for future changes to pront-job.php. So as said better clean up the code in there not to print directly.
An even better approach would be not using dompdf + html at all, but create PDF specifically with a proper layout. This is more work but should give a way cleaner result.

Related

how to generate pdf with dompdf or any other libraries, where html and php are writen together?

Because assigning a value to the "$html" with whole code which is written with php and html is tedious. Is there any way? or can we make whole page as pdf directly?
It really depends on your use case. For example, if the page you want to render to PDF can be accessed via a web server then you can just load that page in your Dompdf script:
// include dompdf then ...
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->load_html_file("http://example.com/document.php");
$dompdf->render();
$dompdf->stream();
If the content and Dompdf logic needs to all be on the same page then you can use output buffering to capture the HTML and feed it to Dompdf:
ob_start();
// HTML + PHP to create output
$html = ob_get_clean();
ob_end_clean();
// include dompdf then ...
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream();

DOMPDF omits second line in rendered PDF

A client of mine had a problem merging two pdf documents. When I looked into it, and ran the PDF file rendered by DOMPDF in a PDF validator, it outputs:
1.1: Header Syntax error, Second line must begin with '%' followed by at least 4 bytes greater than 127
Looking at a valid pdf, this is line 2: %ÓôÌá
The error does not originate from the html, as it occurs even with the default Hello World! example by DOMPDF:
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
Edit: Question is whether there is a way to make Dompdf create the missing line 2?
Should be said that the file can be opened and viewed but error arises when merging with another pdf file.

How to include dompdf without composer

I want to use dompdf in my php website and I try to include the library without Composer and I can not get it to work.
The code I try to test is:
<?php
include "../../plugins/dompdf/Autoloader.php";
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
?>
But i get the error:
Fatal error: Class 'Dompdf' not found ...
Can anyone explain me how to include the library without install composer in the server?
Thank You.
With the right include is working like a charm, as said Samrap i was including the wrong file.
Now the code is:
<?php
//Configure the directory where you have the dompdf
require_once "../../plugins/dompdf/autoload.inc.php";
use Dompdf\Dompdf;
//$dompdf = new dompdf();
//$dompdf = new DOMPDF();
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
?>
Thank you Samrap for your help.
You're requiring the wrong autoload file. The docs clearly state to include this file for autoloading:
require_once 'dompdf/autoload.inc.php';
If you look at that file, you'll see it does require Autoloader.php but performs a few other bootstrapping tasks as well.

How to Create pdf with zend2 and dompdf

I want to use dompdf for generation pdf document.
i got pdf response with follwing code by using DOMPDFModule.But my question is how can i pass variable to the phtml file in order to get print on pdf file my code is as follows
use DOMPDFModule\View\Model\PdfModel;
...
..
public function printAction()
{
$campaignsList=$this->getcampaignTable()->getCampaignList();
$model = new PdfModel();
$model->setOption('paperSize', 'a4');
$model->setOption('paperOrientation', 'landscape');
return $model;
}
How can i print that $campaignList array in print.phtml file
Thanks in advance
I'm not 100% sure what you're asking, you can create the PDF entirely within your action without involving the view(presume this is what you meant when reference your phtml file).
Some example code on PDF generation with DOMpdf :
<?php
// Create a new DOMPDF object
$dompdf = new \DOMPDF();
// Set the paper size to A4
$dompdf->set_paper('A4');
// Load the HTML
$dompdf->load_html($html);
// Render the PDF
$dompdf->render();
// Set the PDF Author
$dompdf->add_info('Author', 'I am the Author');
// Set the PDF Title
$dompdf->add_info('Title', 'My PDF Title');
/**
* 'Attachment' => 1 or 0 - if 1, force the browser to open a
* download dialog, on (1) by default
*/
$dompdf->stream($name,array('Attachment'=>1));
?>
And the documented usage - DOMPDF LINK

dompdf: loading html files to render, doesn't work

dompdf is not able to generate a pdf from a page of my website. However, I've saved the page and uploaded it as simple static html file, and it worked!
So, I don't know if the issue is with the url, or something else.. this is the error I get:
Warning: require_once(/home/o110334/public_html/dompdf/include/firephp.cls.php) [function.require-once]: failed to open stream: No such file or directory in /home/o110334/public_html/dompdf/dompdf_config.inc.php on line 194
Fatal error: require_once() [function.require]: Failed opening required '/home/o110334/public_html/dompdf/include/firephp.cls.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/o110334/public_html/dompdf/dompdf_config.inc.php on line 194
This is the code:
$file = "admin/store/orders/45/invoice/print"; // doesn't work
//$file = "invoice_sample2.html"; //it works (same web page, but stored in a html file)
$dompdf = new DOMPDF();
$dompdf->load_html_file($file);
$dompdf->render();
$dompdf->stream("sample.pdf");
DOMPDF is trying all kinds of stuff/eval's when running local, you're better of trying:
1) the (granted, long way trip) of requesting the HTML by http:
$dompdf->load_html_file('http://yourdomain.ext/'.$file);
2) Don't let DOMPDF eval but use output buffering itself, and let DOMPDF load the resulting string of HTML.
<?php
ob_start();
//be sure this file exists, and works outside of web context etc.)
require("admin/store/orders/45/invoice/print");
$dompdf = new DOMPDF();
$dompdf->load_html(ob_get_clean());
$dompdf->render();
?>

Categories