Set the default page layout of a PDF file with DomPDF - php

I am using DomPDF to generate PDF-Files. This works fine, but I want to set the default page layout which is used by opening the document. I want to have a 100% view when the document is viewed. Actually the page is streched to the max width of the window.
Does anybody know a solution how to set the page layout to "single site"?
Regards,
Lothron

You can do this in dompdf by specifying the view in a meta element, e.g.
<meta name="dompdf.view" content="FitV" />
or
<meta name="dompdf.view" content="XYZ,0,0,1" />
You can also call the set_default_view() method of the canvas class, e.g.
$dompdf->get_canvas()->set_default_view('FitV')
Using the above method can only be done after the PDF document has been instantiated. As of dompdf 0.6.1 that is done after calling $dompdf->render().
Ref: https://groups.google.com/d/msg/dompdf/7L2xMLwmH-Y/Et2mBNny51QJ

Related

Generating a PDF with non-printable background

In Adobe Acrobat there is an option to add a "background" to a PDF file and set the default settings that this image should be visible when opening the document but should not be printed out. I want to automate the process by a PHP script.
I checked all the popular PHP PDF libs (TCPDF, FPDF, mPDF, ...) but none of them seems to provide such an option. All I found is adding images by the ->Image method and place it behind the text. This does work when viewing the document, but of course it is also printed out.
A second approach is to render plain HTML and include custom stylesheets. I created the simple HTML
<h1>Simple text.</h1>
<div>
<p>Should be printed.
<img src="..."></p>
</div>
<div class="no-print">
<p>Should NOT be printed.
<img src="..."></p>
</div>
and saved the CSS in print.css
.no-print {
display: none;
}
and included it by:
<link rel="stylesheet" media=“print” type="text/css" href="print.css">
The result does not show the second div. I guess the PDF libs do not evaluate the media in the link tag. To be honest this approach does not feel right, especially because PDF !== HTML.
Nevertheless I cannot imagine that this is so difficult. How do all the big companies manage this? I'm grateful for every hint!

How to find an image and get path in twig generated content

I am a newbie to PHP Symfony and Twig. I need to dynamically set meta property og:image from the post content, if there is an image. I have a twig code like this.
<div class="content">
{{ d.description|raw }}
</div>
I can and get an image url from the div content if any and set it as a meta property og:image by using javascript but Facebook does not parse JavaScript? Is there a way to do this on server side?
Facebook indeed only reads the source code of the page, so you won't be able to js your way out of it, you have to rely on server side.
You'd have to process the content, in the controller, by using a DOM parser (you can use PHP's own DOMDocument class https://www.php.net/manual/en/class.domdocument.php) and search for an img node (or lack thereof, in case you'll need to provide a default value)
Once you have found the url of the src of the image (user provided or default), pass this as a variable to the view and echo it inside the appropriate meta tag.
Do not forget to print the full path of the image, including the domain and protocol as Facebook now requires it.

Title a PDF page [FPDF]

I'm generating some PDF on my site (with FPDF library) and I can't manage to change the title displayed in the browser (not the one when downloading, but the one corresponding to the HTML)
<title></title>
Have you any idea ?
Yes you can set/change the title by:
$pdf->SetTitle('Title');
Reference: http://www.fpdf.org/es/doc/settitle.htm

PDF Generation Not keeping StyleSheet

So I am using the codeigniter pdf library from: https://github.com/chrisnharvey/CodeIgniter-PDF-Generator-Library
And it works wonders except, It doesn't keep the style sheet when the pdf is generated.
I am trying to use bootstrap to make it look nice, But when I run the script and download the pdf it doesn't have the styling anymore. What do I need to do to keep the stylesheet linked?
My Controller:
public function AdminPracticeSheetLateReport()
{
$this->load->view('pdf/practiceLateReport');
$this->pdf->load_view('pdf/practiceLateReport');
$this->pdf->render();
$this->pdf->stream("welcome.pdf");
}
I am loading the bootstrap stylesheet like so in my view:
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
Is this even possible with this plugin? If not it's quite the crappy plugin if you ask me.
BTW: This library for codeigniter uses the DomPDF
I just checked the source code of codeigniter and noticed that it does not itself make a PDF, but uses DOMPDF in turn (which is another PHP library). codeigniter does not properly send the html and css to DOMPDF, so in my suggestion you should remove codeigniter and use DOMPDF instead. This should correct your problem and also speed up the conversion by a few miliseconds.

Generate PDF of page using alternate/print stylesheet

I want to generate a PDF of a webpage but apply an alternate, print-type stylesheet to it instead of the styles it uses now. Say, for example, I have a button on http://eorailway.co.uk to generate a PDF of the same page (which is run and administered by me, so therefore I can include any PHP/JS necessary to each page) but I want to apply alternate styling to it before generating the PDF.
At the moment I am using the dompdf PHP library to generate the PDF using the normal/default stylesheet, but cannot for the life of me think how to apply the alternate stylesheet to the page when clicking the "Generate PDF" button.
Any advice is most appreciated.
Since the site is under control, you could dynamically decide which stylesheets to include based on a query string parameter. i.e. http://example.com/page.php?stylesheet=print would have your template output only the alternate stylesheet, and your PDF library would fetch that page to generate.
I would recommend making an alternate page with the "print" stylesheet applied and point to it using the print meta tag. (e.g. <link rel="alternate" media="print" href="<? ECHO $_SERVER['PHP_SELF'].'?print'; ?>""> )
Then have PHP determine the stylesheet to use based on the presence of that GET variable.
You can use the DOMXML stuff in php to apply a specific XSLT file to some XML:
$stylsheet = "Example.xsl";
$xsldoc = domxml_xslt_stylesheet_file($stylsheet);
$htmldoc = $xsldoc->process($xmldoc);
$results_page = $xsldoc->result_dump_mem($htmldoc);
That's something I did in php4, might be an easier way in 5.
In the 0.6.0 release of DOMPDF you can specify the stylesheet to use by modifying the DOMPDF_DEFAULT_MEDIA_TYPE configuration constant.
http://code.google.com/p/dompdf/source/browse/trunk/dompdf/dompdf_config.inc.php?r=336#234

Categories