I'm using mpdf and writing a bunch of HTML to the pdf object via mpdf::writeHTML(). Is there any way, rather than outputting an PDF, to simply dump it right back to the browser instead? So, rather than creating the PDF just write it out as a web page?
I want to give the user the option of a PDF or a web page, and rather than branching off for echo or writeHTML for each line, I'd like to build the document and then either output web or PDF.
EDIT TO ADD:
Something like this:
$mpdf = new mpdf();
$mpdf->writeHTML( "<p>Hello World</p>" );
$mpdf->addPage( 'L' );
$mpdf->writeHTML( "<p>Lorem ipsum egg foo yung.</p>" );
if( $_GET['format'] == 'pdf' ) {
$mpdf->output(); //spit out a PDF
} elseif ( $_GET['format'] == 'web' ) {
echo $mpdf->contents_as_html(); // write a web page
}
I'm currently writing each line to a giant string, and then either passing the string to mpdf::writeHTML() or echo; but this doesn't allow me to use various mpdf functions such as addPage(), bookmark(), and so forth.
This is what I did, per a suggestion by #CBroe. When writeHTML() is called, it writes to an internal variable $this->strHTML, and then does its normal process. If the object is cast to string, it returns $this->strHTML
class myPDF extends Mpdf {
private $strHtml = '';
public function writeHTML( $html, $mode = 0, $init = true, $close = true ) {
$this->strHtml .= $html . "\n";
return parent::writeHTML( $html, $mode, $init, $close );
}
public function __toString() {
return $this->strHtml;
}
}
You can choose the output with mPDF by changing the second parameter.
I = send the file inline to the browser.
F enter code here= save to a local file with the name given by $filename.
S = return the document as a string. $filename is ignored.
D = send to the browser and force a file download with the name given by $filename.
Output mPDF:
$mpdf->Output($filename, "I"); // Change "I" to your preferred output
If you choose output the file in your browser, just make sure that you target the output on an empty page. Otherwise it's possible that the header and footer will interfere.
Artikel mPDF output:
https://mpdf.github.io/reference/mpdf-functions/output.html
Related
I am using HTML2PDF library in codeigniter.I am trying to generate bulk pdf using it.
In which i am facing issue like same content in every pdf or pdf have no content.I have already did my homework.Yeah but there is always showing perfect for generated first pdf (For account :3)
As per me there is must be issue of below code :
ob_start();
require_once($template_config.'template.php'); //
$content = ob_get_contents();
ob_clean();
Issue : It works for first time but for second time it flush all the content of content variable and due to that duplicate PDF or without content PDF generate.
I have tried like below
1) create object in generatetemplate.php and pass to common.php
2) tried with include_once //getting same conent in every pdf and if i am doing echo then showing no content for 2nd and 3rd pdf
File structure :
application
controllers
generatetemplate.php
libraries
common.php
html2pdf
html2pdf.php
template.php
common.php :
function print_content($customerdata){
$this->load->library('/html2pdf/html2pdf');
$template_config=$this->config->item('template');
ob_start();
require_once($template_config.'template.php'); //
$content = ob_get_contents();
ob_clean();
$content = str_replace("<CUSTOMER_ADDRESS>",$CUSTOMER_ADDRESS,$content);
$this->CI->html2pdf->pdf->SetDisplayMode('fullpage');
$this->CI->html2pdf->writeHTML($content);
$this->CI->html2pdf->Output($download_path,"F");
}
generatetemplate.php
function __construct() {
parent::__construct();
$this->load->library("common");
$this->load->library('html2pdf');
}
function get_customer_data(){
$this->db->order_by("id","DESC");
$this->db->where('id IN (1,2,3)');
$query = $this->db->get("customers")->result_array();
foreach($query as $key=>$accountdata){
$this->common->print_content($accountdata);
}
}
Any help and ideas will be appreciated.
I have tried below code and its work for me.
Common.php
function print_content($customerdata){
$this->load->library('/html2pdf/html2pdf');
$template_config=$this->config->item('template');
ob_start();
require_once($template_config.'template.php'); //
$content = ob_get_contents();
ob_clean();
$content = str_replace("<CUSTOMER_ADDRESS>",$CUSTOMER_ADDRESS,$content);
$this->CI->html2pdf = new HTML2PDF('P','A4','en'); // Just added this line and its work for me.
$this->CI->html2pdf->pdf->SetDisplayMode('fullpage');
$this->CI->html2pdf->writeHTML($content);
$this->CI->html2pdf->Output($download_path,"F");
}
I'm using Code Igniter framework, and tcpdf third-parties
I have a global variable in controller :
public $pdf_content = '';
here is some of my function to book :
$data['success'] = $this->hotel_model->set_hotel(); //get the data from database
//view reservation detail
$this->load->view('hotel/header');
$this->load->view('hotel/success', $data );
$this->load->view('hotel/footer');
//assign the HTML page into global variable
$this->pdf_content = $this->load->view('hotel/success', $data , TRUE);
In my view file I'm using a button to download the HTML as pdf file, here is the download function :
public function download(){
$this->load->library("Pdf");
$this->load->helper('pdf_helper');
$pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
// Add a page
$pdf->AddPage();
$pdf->writeHTMLCell(0, 0, '', '', $this->pdf_content , 0, 1, 0, true, '', true);
$pdf->Output('invoice.pdf', 'I');
}
Both function located inside the same controller.
I think there is nothing wrong in my code based on any tutorial I ever read.
But it downloaded some blank page instead, and when I try to assign string value into $pdf_content, it works fine, The string value is written into the downloaded PDF file.
Anybody know what I missed there?
Or there is something wrong in my code?
Any help is appreciated.
There are multiple ways. The easiest would be to save the page as pdf manually (ctrl+p) and then make it available as download. OR, you can show the page and run the following script in the bottom of the body.
<script>
var yourName = "someName';
document.title = yourName;
window.print();
</script>
This fires the ctrl+p command and allows users to either print or save the page.
I have a CMS where users can create and edit their own content in their websites. I also provide the possibility to include forms and galleries by simply replacing specific Div's in their content.
In the past I simply exploded the content on these Div's to an array, replaced the whole Div's with the needed html code (by using PHP's include) to show the form or gallery at that exact position, imploded the whole array to a string again (html) and used in the website.
Now I am trying to achieve the same in Laravel 5:
// example plugins div in HTML
// ******************************
<div class="plugin form"></div>
// PageController.php
// ******************************
$page = Page::where(('url', '=', "home")->first();
$page->text = Helpers::getPlugins($page->text);
// Helpers.php (a non default custom class with functions)
// ******************************
public static function getPlugins($page)
{
$dom = new DOMDocument();
$dom->loadHTML($page, LIBXML_HTML_NOIMPLIED);
$x = $dom->getElementsByTagName("div");
foreach ($x as $node)
{
if (strstr($node->getAttribute('class'), "plugin"))
{
$plugin = explode(" ",$node->getAttribute('class'));
$filename = base_path() . "/resources/views/plugins/" . trim($plugin[1]) . ".blade.php";
if (is_file($filename))
{
ob_start();
include($filename);
ob_get_contents();
$node->nodeValue = ob_get_clean();
}
else
{
$node->nodeValue = "Plugin <strong>".$node->getAttribute('class')."</strong> Not found</div>";
}
}
}
return $dom->saveHTML();
}
Sofar so good, the content is returned but what I get is all the pure text blade markup instead of the Laravel generated html which I want to use.
I think there is a way this could work but I cannot come to think of it.
Try manually building the template by using the method BladeCompiler->compile(), read more here
Edit: I think the facade Blade::compile() will give you access to this function too, just add use Blade at the top of the file.
Is it possible to "merge" or "paste" a PDF-file into antother PDF? Or must it be a image instead?
The PDF i want to to paste or merge, is a simple picture that is going to appear at the bottom of the finished PDF:
//Generate the "Original" PDF here..
function addReklam($reklamblad) //The PDF that should be merged into the PDF that is created above
{
//Count how many pages that has been created, and add it at the bottom of the PDF:
if($this->drawed_lines<52)
{
$this->active_page = $this->pdf->pages[2];
}
elseif($this->drawed_lines<92)
{
$this->active_page = $this->pdf->pages[3];
}
elseif($this->drawed_lines<132)
{
$this->active_page = $this->pdf->pages[4];
}
else
{
$this->active_page = $this->pdf->pages[5];
}
//$this->active_page = $this->pdf->pages[5]; // page 5 is the last
//Add it here???
}
My recommendation would be to use the Zend_Pdf::load() method to load the "Original" PDF file into a local instance of Zend_Pdf and then you can access the pages using the pages[] array as in your sample code and use the all the standard functions like drawImage() etc to make the needed modifications prior to saving the updated version.
I have created a pdf file with FPDF in PHP. When i insert the header and footers in it, they automatically gets displayed on all the pages of the pdf file. But i want to stop these header and footer from getting displayed on the first page and display them starting from the second page of the pdf file. I have searched the net but unable to find a solution.
In other words i want to dynamically create a cover page for the pdf report i have created with FPDF.
Can anybody give me some tips on how to perform this task of hidinh header and footer from the first page in pdf file!
Any help will be appreciated!
That's an easy task. Try the following:
class PDF extends FPDF {
...
function Header() {
if ( $this->PageNo() !== 1 ) {
// Add your stuff here
}
}
function Footer() {
if ( $this->PageNo() !== 1 ) {
// Add your stuff here
}
}
}
The problem is the Footers are created in Close() method at line 288 which is called from Output() at line 987 what means you're effectively turning the Footer off and then on just to display it anyways. What I would do if I needed the flexibility is something like:
class PDF extends FPDF {
function Header() {
if (!isset($this->header[$this->page]) || !$this->header[$this->page]) {
// ...
}
}
function Footer() {
if (!isset($this->footer[$this->page]) || !$this->footer[$this->page]) {
// ...
}
}
}
and then use it like:
$pdf->header[1] = false;
$pdf->footer[1] = false;
$pdf->AddPage();
$pdf->header[2] = true;
$pdf->footer[2] = true;
$pdf->AddPage();
It might be not the most elegant solution, but it works and it effectively allows you to change the visibility of the footers dynamically (p.s.: not specifying the state would also leave you with headers on effectively reducing the amount of code you need)
I'd like to add an answer for people coming here that don't want to skip the first, but the last (or any) page. Especially handy if you have dynamically changing text and cant foresee page numbers.
This can be done by setting a boolean while adding the page to the PDF.
Define your Header / Footer as
class PDF extends FPDF {
function Header() {
if (!$this->skipHeader) {
// ...
}
}
function Footer() {
if (!$this->skipFooter) {
// ...
}
}
}
Then, when initializing the pdf make sure to set these bools to false, so you will get headers/footers in general.
$pdf = new PDF();
$pdf->skipHeader = false;
$pdf->skipFooter = false;
Once you actually want to skip a Header or Footer, set the respective bool to true
$pdf->AddPage();
$pdf->skipHeader = true;
$pdf->AddPageContents();
Remember to set them back to false if you want headers/footers on the next page!
As an extension of what Paul's said, the footer is rendered after any content, so set skipFooter to true after rendering content.
$pdf->AddPage();
$pdf->skipHeader = true;
$pdf->AddPageContents();
$pdf->skipFooter = true;