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();
?>
Related
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.
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.
I am using DOMPDF to generate PDFs from HTML.
I have copied all required files from github (encoding branch). But it says class DOMPDF not error as below.
link for dompdf_config.inc.php in gitbub : https://github.com/dompdf/dompdf/tree/encoding
Here is my code :
require_once("APIs/dompdf-encoding/dompdf_config.inc.php");
$cart_body='<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>New Order Placed</title></head><body><p>Test Printing...</p></body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($cart_body);//body -> html content which needs to be converted as pdf..
$dompdf->render();
$dompdf->stream("sample.pdf"); //To popup pdf as download
Actual Output is :
Fatal error: Class 'DOMPDF' not found in /home/web/www/test_dompdf.php
on line 30
Line 30 is $dompdf = new DOMPDF();
Note: Other Master Branch is working fine. I need this encoding branch as it solves encoding font related issues.
I've tested your code and it works fine for me - sample.pdf file is being downloaded in browser. I've downloaded library from https://github.com/dompdf/dompdf/releases/tag/v0.6.1 url (not only the encoding branch(
Probably you haven't moved the whole project to selected directory or you haven't downloaded the whole library. I moved the whole downloaded directory content to APIs/dompdf-encoding directory and I have here files dompdf_config.inc.php and directories lib, include and www.
EDIT
As you edited you want to use only encoding branch, what you have to do is adding the following code at the beginning of your file:
use Dompdf\Adapter\CPDF;
use Dompdf\Dompdf;
use Dompdf\Exception;
EDIT2
The whole working code:
<?php
use Dompdf\Adapter\CPDF;
use Dompdf\Dompdf;
use Dompdf\Exception;
require_once("APIs/dompdf-encoding/dompdf_config.inc.php");
$cart_body='<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>New Order Placed</title></head><body><p>Test Printing...</p></body></html>';
$dompdf = new Dompdf();
$dompdf->load_html($cart_body);//body -> html content which needs to be converted as pdf..
$dompdf->render();
$dompdf->stream("sample.pdf"); //To popup pdf as download
I have also changed DOMPDF to Dompdf just in case (in Windows both are working)
This solution works for version 2.0.2.
<?php
require __DIR__ . '/vendor/autoload.php';
use Dompdf\Dompdf;
$html =
'<html><body>' .
'<p>Put your html here, or generate it with your favourite ' .
'templating system.</p>' .
'</body></html>';
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->render();
$output = $dompdf->output();
file_put_contents('/tmp/Brochure.pdf', $output);
i converted my html to Pdf using dompdf and i want to print it. My problem is how to open my pdf file in print window instead of opening the download dialog?
include('dompdf/dompdf_config.inc.php');
$savein = 'pdfdirectory/';
$html = " my htmlcode here "
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->output();
file_put_contents(($savein.'file.pdf'), $pdf);
$dompdf->stream('file.pdf');
You can't force the user to open the file in their browser, but you can specify options for the stream() method that hint to the browser whether or not to download the file. The default is to tell the browser that the file is an "attachment" which usually translates to a download. But if you modify the last line of your code as follows most browsers should display the PDF in the browser:
$dompdf->stream( 'file.pdf' , array( 'Attachment'=>0 ) );
After creating a PDF file in PHP Using DOMPDF, I need to redirect user to different page.
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($fileName . '.pdf', array("Attachment" => 0));
header('location:newpage.php');
This code creates the PDF file but does not redirect. How can I fix this problem?
Note : This is my previous question. Still I'm trying to get this fixed, still no luck.
The $dompdf->stream() call starts outputting data to the client. Once content is being sent, it is impossible to modify the headers, and thus perform the redirection you want. You can see this by turning on the warnings in PHP (error_reporting).
A way to do this is to open a new window, but it requires Javascript.
function downloadPDF(){
window.open("get-pdf.php?id=12345");
location.href = "newpage.php";
}