I'm not able to get my PDF printed if I add a logo in my template_header.php file.
If I enable include_once("template_header.php") it's not generating any PDF, and it reports this error message:
PHP Fatal error: Call to undefined function imagecreatetruecolor()
If I disable my header (i.e. above the include_once line) it's generating a PDF without a logo and containing text only.
I tried enabling below, but the same problem persists:
define("DOMPDF_ENABLE_REMOTE", true);
My dompdf ver. is dompdf_0-6-0_beta3.
Can anyone help me on this?
Below is the updated code
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$html = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>booking</title>
<link rel="stylesheet" href="style/style.css" type="text\css" media="screen" />
</head>
<body>
<div align="center" id="mainwrap">
<?php include_once("template_header.php")?>
</div>
</body>
</html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
Please check your GD library installation in php.
Run <?php phpinfo(); ?> in a php file and check for GD library then
Update -
Check your php.ini and look for extension=php_gd2.dll
If ; [commented] then un comment it and restart the services then.
Related
I'm working on a project that needs to convert an HTML page to PDF.
I am using dompdf library in my project, however, I'm having trouble with embedding custom fonts in the PDF preview. I was expecting pdfdom will automatically embed any attached fonts like it normally work in HTML. Or is there any configuration needed first?
Here's the HTML content
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15">
<link href='http://fonts.googleapis.com/css?family=Give+You+Glory&v2' rel='stylesheet' type='text/css'>
</head>
<body>
<h2>Give You Glory</h2>
<p style="font-family: 'Give You Glory', sans-serif;">
Grumpy wizards make toxic brew for the evil Queen and Jack
</p>
</body>
</html>
And here's how the library usage:
$dompdf = new Dompdf();
$dompdf->setPaper('A4', 'landscape');
$dompdf->loadHtml($html_content);
$dompdf->render();
$dompdf->stream("testing.pdf", array("Attachment"=>0));
My PDF only displays after I refresh the page, otherwise it turns into weird symbols. When it does display, its fullscreen (header disappears) and when I press go back, the URL changes, but the page still displays the DPF, and when I refresh, this fixes itself again.
Im also using JQuery Mobile, maybe this has something to do with it.
Here is some code:
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hondensectie</title>
<link rel="stylesheet" href="themes/themerollertest.min.css" />
<link rel="stylesheet" href="themes/jquery.mobile.icons.min.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<link type="text/css" rel="stylesheet" href="css/style.css"/>
This is the head ^^
<div data-role="page" data-theme="a">
<div data-role="header" style="overflow:hidden;" data-position="fixed">
<h1>PDF Maker</h1>
Back
Home
</div>
<div data-role="content" data-theme="a">
<div data-role="collapsible" data-content-theme="a">
<h4>Gebruikers</h4>
<p>
<section id="hondppp" data-ajax='false'>
<?php
ob_start();
//call the FPDF library
require('fpdfmap/fpdf.php');
//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm
//create pdf object
$pdf = new FPDF('P','mm','A4');
//add new page
$pdf->AddPage();
$pdf->Output();
ob_end_flush();
?>
</section>
</p>
</div>
</div>
Right before you call the output function use the ob_end_clean function to remove and discard any other output that may have already been sent to the browser.
$pdf = new FPDF('P','mm','A4');
$pdf->AddPage();
ob_end_clean(); // clear out anything that may have already been output
$pdf->Output();
When you want to display a PDF file, the server has to return a response that contains Content-Type: application/pdf header in it. As far as I know, FPDF sets this header when Output function is called. However, no headers can be set after the response is sent, and this usually happens with the first line of HTML being displayed with your PHP script. Actually I'm surprised that you can ever see the PDF, and that you don't get the infamous "headers already sent" error (although that one probably shows somewhere in the logs, if you know where to look).
The point is, don't write any HTML before or after the PDF content. Your entire PHP script should just contain these lines:
//call the FPDF library
require('fpdfmap/fpdf.php');
//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm
//create pdf object
$pdf = new FPDF('P','mm','A4');
//add new page
$pdf->AddPage();
$pdf->Output();
I want to create a pdf on my wordpress homepage with dompdf. Local an my computer with xampp there is no problem, all works fine.
On wordpress I am using the plugin "PHP Code For Posts". The dompdf folder I copied to the folder of the plugin.
The pdf get created but it is defective.
<?php
if(isset($_POST['button']))
{
require_once("dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
function pdf_create($filename, $paper, $orientation, $stream=TRUE)
{
$dompdf = new DOMPDF();
$dompdf->set_paper($paper, $orientation);
$dompdf->load_html('TEST');
$dompdf->render();
$dompdf->stream($filename.".pdf");
}
$filename = 'file_name';
$dompdf = new DOMPDF();
pdf_create($filename, 'A4', 'portait');
}
?>
<form method="post" >
<p>Test: <input type="text" name="price" /></p>
<p><input type="submit" name="button" value="PDF" /></p>
</form>
I hope anybody can help me. I have no idea why it works local and on wordpress it creates a defective file.
UPDATE:
Here is the code when I open the defective file with notepad.
<!DOCTYPE html>
<html lang="de-DE">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="http://test-homepage.de/xmlrpc.php">
<!--[if lt IE 9]>
<script src="http://test-homepage.de/wp-content/themes/zerif-pro/js/html5.js"></script>
<link rel="stylesheet" href="http://test-homepage.de/wp-content/themes/zerif-pro/css/ie.css" type="text/css">
<![endif]-->
<title>Test – test-homepage</title>
<link rel="alternate" type="application/rss+xml" title="test-homepage » Feed" href="http://test-homepage.de/feed/" />
<script type="text/javascript">
window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/72x72\/","ext":".png","source":{"concatemoji":"http:\/\/test-homepage.de\/wp-includes\/js\/wp-emoji-release.min.js?ver=4.5.3"}};
This may be a solution if you want to stick to the currently used Wordpress plugin and output buffering is enabled (both webserver and php.ini may control output buffering; see the output buffering section on the PHP.net page for more details.
<?php
if(isset($_POST['button']))
{
// make sure that output buffering has been enabled
if (ob_start()) {
// clear previous contents from the server output cache
ob_clean();
try {
require_once("dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
$filename = 'file_name';
$paper= 'A4';
$orientation= 'portrait';
$dompdf = new DOMPDF();
$dompdf->set_paper($paper, $orientation);
$dompdf->load_html('TEST');
$dompdf->render();
$dompdf->stream($filename.".pdf");
// make sure the content of the buffer is sent to the client.
ob_flush();
// terminate the script to make sure no additional content is sent.
exit(0);
} catch (Exception $ex) {
error_log(sprintf("PDF generation failed with %s in line %d", $ex->getMessage(), $ex->getLine()));
}
} else {
// log a message in default error log
error_log("Output buffering could not be enabled (PDF routine)");
}
}
?>
Debugging your setup
As you said you are pretty new to PHP, please create a file with the content:
<?php
phpinfo();
Upload the file to your server and open the file with your browser, scroll down until you find "Configuration" > "PHP Core". In the list below, search for the entry "output_buffering". If it is set to "0", output buffering has been disabled via php.ini. If it has been disabled, all output of your page is directly send to the client and there is no programmatic way to do what you achieve without changing the server setup.
There may be several ways to enable buffering, i.e. your provider may allow changing certain settings via web configuration software or you may add an .htaccess file to your web directory to enable it as described here.
Another update
As you already found out, output buffering has been enabled properly, so we should be able to modify the output buffer to create the PDF even if output had been served before. I copied your code, ran the PHP part after some output had already been sent to the browser and the same happened as on your web page, the static HTML content was also included in my PDF document. The main problem will be that Wordpress does not use output buffering internally and sends the output directly to the browser.
Here's what I did that resolved the problem -> start the output buffering as soon as possible, inside your index.php:
<?php ob_start(); ?>
After buffering had been enabled, I was able to control the buffer and generate the PDF file as required.
Further diagnosis
I just want to make sure we are not missing something, could you add this to your PDF generation code:
if(isset($_POST['button']))
{
$sent = headers_sent($file, $line);
die("The output started in {$file} and at line {$line}");
}
I am trying to serve an image to html via php script. But I am stuck and its not working :-(
Below is my image.php script
<?
$_GET['f'] = 'all_three.jpg';
$image = null;
$image = file_get_contents($_GET['f']);
header("Content-Type: image/jpeg");
echo $image;
?>
and below is my index.html
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<title>Page Title</title>
</head>
<body>
<img src="image.php" alt="Image" />
</body>
</html>
The link to image is "hard-coded" inside the php, all I am doing is calling "image.php" to serve that hardcoded image and it does not work! What am i doing wrong?
when I view->source after loading image.php, i can see the php script
Either:
You are depending on short tags, but your server is configured to not support them.
Their use is not recommended, stop using them. Use <?php instead of <?
Or:
Your server does not support PHP. Install PHP on it.
The error is on Content-Encoding, what's Text editor are you use ? Notepad 6.x ?
If yes please try:
Encoding > Encode in UTF-8 without BOM
BOM make php file have a more content and image doesn't accept another content else image
I am having problems with producing a Rectangle with GD - PHP. I am running XAMPP and programming on Netbeans IDE. I am new to PHP but some what fimilar with Perl.
<?php
header ("Content-type: image/png");
$newImg = ImageCreate(250,250);
$red = ImageColorAllocate($newImg,255,0,0);
ImageFill($newImg,0,0,$red);
ImagePNG($newImg);
ImageDestroy($newImg);
?>
Here is the out put.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
‰PNG
IHDRúú²"È~PLTEˆÁÿ·¿~`IDAThíÁ‚ ÿ¯nH#¿ :F¨z’IEND®B`‚ </body>
</html>
You need to stuff the html into a seperate file, and use a <img> tag to load your php script, e.g.
<html>
<body>
<img src="yourimagescript.php" />
</body>
</html>
alternative, you COULD use data uris to embed the image directly into the html, but that brings about another set of problems, e.g.
<img src="data:image/jpeg;base64,<?php echo base64_encode(imagepng($newImg, '-')) ?>/>
If you want to use binary as source for image you need to use it as follows:
<img src="data:image/png;base64,data..."/>
or if you want use it as it is in your example you need to point image src attribute to your php script from example
<img src="path/to/script.php"/>
and path/to/script.php"/ displays only data and correct header
‰PNG
IHDRúú²"È~PLTEˆÁÿ·¿~IDAThíÁ‚ ÿ¯nH#¿ :F¨z’IEND®B‚