DOMPDF omits second line in rendered PDF - php

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.

Related

PDFs generated with Dompdf library filling up tempfolder

I am currently generating some pdfs with the dompdf library but every time a pdf is being generated my temp folder is being filled with this file and the images associated with it. I am trying to unlink or delete the pdf and any temp files generated with it but the following code is not giving the desired result. Does anyone know what else I can do in this situation?
I am using PHP and have installed the library with composer.
pdf code:
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
//$dompdf->loadHtml('hello world');
$dompdf->set_option('isHtml5ParserEnabled', true);
$dompdf->set_option('isRemoteEnabled', true);
$dompdf -> loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
unlink($dompdf);

dompdf placing same font in full bodytag

I have installed tangerine in my dompdf. Now what is happening is when i try on my windows machine it works fine. But in ubuntu when i put the same code in xampp the generated pdf is massed up. I used the tangerine font with only my h3 tags but in the generated pdf the font style is placed in whole body. Here is my dompdf settings
<?php
// Include autoloader
require_once 'dompdf/autoload.inc.php';
// Reference the Dompdf namespace
use Dompdf\Dompdf;
// Instantiate and use the dompdf class
$dompdf = new Dompdf(array('isPhpEnabled' => true));
// Load content from html file
ob_start();
include_once 'pdfcontent.php';
$output = ob_get_clean();
$dompdf->loadHtml($output);
//$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('b4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("funeral",array("Attachment"=>0));
?>
Well the the problem was basically with the cached dompdf file. I have deleted the dompdf folder and uploaded again. Now it works fine.

php to pdf using dompdf

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.

DOM PDF generate only 1 page pdf skip remaining content

I have the following code which uses DOM PDF Library for converting Html content to PDF file.
$dompdf = new DOMPDF();
$dompdf->set_paper(array(0,0,450,306));
$dompdf->load_html($html);
$dompdf->render();
$output = $dompdf->output();
file_put_contents($pdf_path, $output);
But it generates 2-3 pages based on the html content.
I wanted to limit it to single page only and skip the remaining content.
The easiest way is to render to image. The GD adapter allows you to specify the page you want to render.
dompdf 0.6.2 or earlier: set DOMPDF_PDF_BACKEND to "GD" which only renders the first page of the document.
dompdf 0.7.0 or later: $dompdf->set_option('pdfBackend', 'GD');. This release renders all pages and allows you to specify the page to output/stream (default is to send the first page).
If you really need to render only the first page to a PDF that's a bit more difficult. Though technically possible to do this only with dompdf I'd actually be inclined to fully render the PDF then use an external library to pull out just the first page.
For example, with libmergepdf you could do it like this:
use iio\libmergepdf\Merger;
use iio\libmergepdf\Pages;
use Dompdf\Dompdf;
$m = new Merger();
$dompdf = new Dompdf();
$dompdf->load_html('...');
$dompdf->render();
$m->addRaw($dompdf->output(), new Pages('1'));
file_put_contents('onepager.pdf', $m->merge());
(specific to dompdf 0.7.0, but code is nearly the same for 0.6.2)

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

Categories