Page break in Html2Pdf - php

I am in the process of generating a dynamic pdf file, which contains data of around 10,000 users, in general the app is developed using MySQL and PHP. The dynamic content is so heavy that, I found it difficult to process with fpdf() class. So I converted my output PHP page as a HTML file using ob_get_clean(). Now the html file is generated successfully and also the pdf file. But i want to leave a page break after each user's data, that is every user's data must start in a fresh page. I couldn't use any HTML tags because, in the dynamically generated HTML file, everything is out of the <html> and </html> tags. Please help me so that some how i make a page break in the pdf file after every user's data... Thanks in advance :)

html2pdf supports page tag:
protected function _tag_open_PAGE($param) {}
at line 2229. You can see there what attributes are supported. For example following creates one page in landscape and one in portrait mode:
<page orientation="l">
... some content ...
</page>
<page orientation="p">
... some content ...
</page>

Basing on macdabby's work (which doesn't work). But thanks to him, the idea is correct.
Html2Pdf v4.03
For example we want to parse a tag DIV:
html2pdf.class.php line 2948:
protected function _tag_close_DIV($param, $other='div')
{
if ($this->parsingCss->value['page-break-after'] == "always")
$this->_setNewPage(null, '', null, $this->_defaultTop);
$this->parsingCss->setPosition();
...
}
parsingCss.class.php Line 114:
//add a new style declaration
public function initStyle()
{
...
$this->value['page-break-after'] = null;
}
Line 1024 add a new handler to the switch case:
case 'page-break-after':
$this->value[$nom] = $val;
break;
And then for it to work, your html content should contain the break element
<div style="page-break-after:always; clear:both"></div>
Watch out for case sensitive style, not sure if the plugin handle it

i just figured this out after having the same problem. the parser that they use DOES support the page-break-after tag, but the html2pdf does not work.
i think i have it working by doing the following modifications to html2pdf.class:
around line 4174, the first thing inside:
protected function _tag_close_P($param){
should be:
if($this->parsingCss->value['page-break-after'] == "always")
$this->_setNewPage();
around line 2961, the first thing inside:
protected function _tag_close_DIV($param, $other='div'){
should be:
if($this->parsingCss->value['page-break-after'] == "always")
$this->_setNewPage();

You possibly want to use some css, eg:
h1 {page-break-before:always}

Related

Create PDF Laravel 5.5?

I want to create a pdf page from html file in Laravel but I do not know how to do it. You can help me
https://github.com/barryvdh/laravel-dompdf
// Import in top of controller file
use Barryvdh\DomPDF\Facade as PDF;
// use below code to generate and download pdf file
$pdf = PDF::loadView('bladefilename', ['arrayname' => $array]);
return $pdf->download('filename.pdf');
Write Above code in the controller file
In Blade file (HTML file) set array data.
Show Below Link : https://github.com/barryvdh/laravel-dompdf/issues/126
I know I am a little bit late but hopefully this will help someone.
There are 2 ways:
Create HTML page and use jspdf to print this page. The best way is to use html2pdf: https://github.com/eKoopmans/html2pdf.js
You can use this one to convert html to canvas and print to pdf. All css will be kept but you have to change the page to fix the space between pages as there may be some bad cut at the end of each page.
Print the page using latex. You have to use laravel latex compiler: https://github.com/fvhockney/latexcompiler
It is a little bit harder to styling the page but there is no problem with paging.

How to push the title of the content to the next page in PDF

I have coded PHP script to generate PDF with text contents using TCPDF library. First, the script gets the contents from database and creates temporary .html file. Then the script gets the contents from the .html file and writes to create PDF document.
However, the problem here is it doesn't know when to break a page. So, it looks something like in the image.
I want the script to break the page when the title comes at the bottom of the page and move it to the next page.
There is a function called $pdf->AddPage(); that breaks the page.
Is there any solution to this? Please help.
Have you tried page-break-after CSS property ? Add this to the DIV which is just above the title. So the style of the above DIV will look something like this.
.DIV_CLASS {
page-break-after: always;
}
The page-break-after property sets whether a page break should occur
AFTER a specified element or not.
always value of the property inserts a page break after the element.
Update:
To make sure your particular section/DIV doesn't get divided between pages. You can make use of page-break-inside property.
Use it like this,
.DIV_CLASS {
page-break-inside: avoid;
}
Above CSS will make sure that DIV with class DIV_CLASS will never get divided among pages.
The page-break-inside property sets whether a page break is allowed
inside a specified element.
avoid value of property avoids page break inside the element (if possible)

Notepad++ function list PHP not working if I didn't close the PHP tag

I want to use function list feature from notepad++ for my PHP code
Actually the function list is working normally if I write the close PHP tag ( ?> ).. But if I didn't close the PHP tag, the function list is not working
I just want, the function list is still working even I didn't close the PHP tag
See the image
On the top is without close PHP tag
On the bottom is with close PHP tag
Then I read this, notepad++ function list doc
https://notepad-plus-plus.org/features/function-list.html
I guess, I need to edit the regex.. But the problem is I can't edit the regex because the regex looks like too complex for me
This is the regex and xml of PHP function list
<parser id="php_function" displayName="PHP" commentExpr="((/\*.*?\*)/|(//.*?$))">
<classRange
mainExpr="^[\s]*(class|abstract[\s]+class|final[\s]+class)[\t ]+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*([\s]*|[\s]*(extends|implements|(extends[\s]+(\\|[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+[\s]+implements))[\s]+(\,[\s]*|(\\|[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*))+[\s]*)?\{"
openSymbole = "\{"
closeSymbole = "\}"
displayMode="node">
<className>
<nameExpr expr="(class|abstract[\s]+class|final[\s]+class)[\s]+[\w]+"/>
<nameExpr expr="[\s]+[\w]+\Z"/>
<nameExpr expr="[\w]+\Z"/>
</className>
<function
mainExpr="^[\s]*((static|public|protected|private|final)*(\s+(static|public|protected|private|final))+[\s]+)?(function[\s]+)+([\w]+([\s]+[\w]+)?([\s]+|\*[\s]+|[\s]+\*|[\s]+\*[\s]+))?([\w_]+[\s]*::)?(?!(if|while|for|switch))[\w_~]+[\s]*\([^\{]*\{">
<functionName>
<funcNameExpr expr="(?!(if|while|for|switch))[\w_]+[\s]*\([^\{]*"/>
<!-- comment below node if want display method with parmas -->
<funcNameExpr expr="(?!(if|while|for|switch))[\w_]+"/>
</functionName>
</function>
</classRange>
<function
mainExpr="^[\s]*function[\s]+\w+\("
displayMode="$className->$functionName">
<functionName>
<nameExpr expr="(?!(if|while|for))[\w_]+[\s]*\("/>
<nameExpr expr="(?!(if|while|for))[\w_]+"/>
</functionName>
<className>
<nameExpr expr="[\w_]+(?=[\s]*::)"/>
</className>
</function>
</parser>
Can someone help me
Thank you
[Solved]
We just need to add enter/add new line after close the class or function tag
So maybe this is a bug
Thanks
For me the problem was that there was no closing PHP tag.
May be that helps someone.
Add a "?>" closing PHP tag if it is already not there.
There are several reasons why function list can't render function tree.
In older versions of Notepad++ PHP section closing tab is required. Just simple add ?> at the end of file. Sometimes after closing tab new line is required.
functionList.xml incorrect parse PHP file. For example this function description broke function list rendering:
/**
* Unset an instance of this class.
*
* #param Spreadsheet $spreadsheet Injected spreadsheet identifying the instance to unset
*/
public function __destruct()
{
$this->workbook = null;
}
In this case you must change (or add) commentExpr In functionList.xml in %APPDATA%
(There are two functionList.xml locations. Program Files directory and %APPDATA% directory)
<parser
id ="php_syntax"
displayName="PHP"
commentExpr="(?'MLC'(?s-m)/\*.*?\*/)|(?'SLC'(?m-s)(?:#|/{2}).*$)|(?'STRLIT'(?s-m)"[^"\\]*(?:\\.[^"\\]*)*"|&apos;[^&apos;\\]*(?:\\.[^&apos;\\]*)*&apos;)"
>
The internal "function list" does not work at all for me. I tried to delete one or both "functionlist.xml" files, which was recommended at some sites, but nothing worked for me.
I prefer the plugin "functionList" from
https://sourceforge.net/projects/npp-plugins/files/Function%20List/
with the Patch from
https://github.com/gwarnants/FunctionList-PHP-Patch
which works great with my Notepad++ 6.9.2
Replace in functionList.xml, the php parser. Use this new one you can find in the notepad++ forums ->
https://notepad-plus-plus.org/community/topic/15124/php-function-list-and-abstract-functions/17
it can handle abstract class and functions.

Getting information from ID's

First off, I'm brand new to PHP so I'm sorry if this is a stupid question, second of all sorry if this title is incorrect.
Now, what I'm trying to do is create an overlay for a game that I play. My code for the overlay works perfectly, and now I'm working on my HTML file which gets its information from a website and outputs it. The code on the website looks like this:
<span id="example1">Information I want</span>
<span id="example2">More Info I want</span>
...
<span id="example3">And some more</span>
Now what I want to do is create a PHP script which goes in and finds elements by their names and gives me the information in those span tags. Here's what I've tried so far, it's not working however (no surprise):
//Some HTML here
<?php
$doc = new DomDocument;
$doc->validateOnParse = true;
$doc->Load('www.website.com');
echo "Example1: " . $doc->getElementById('example1') . "\n";
?>
//More HTML
To be honest, I have no clue what I'm doing. If anyone could show me an example of how to do this properly, or to point me in the right direction I would appreciate it.
The text between open and close tags is a Text Node.
Just write $doc->getElementById('example1')->nodeValue
Your code seems along the right lines, but you're missing a few things.
First of all, your load call is literally looking for a file named "www.website.com". If it's a remote file, you must include the http:// prefix.
Then, you are attempting to echo out the node itself, whereas you want its value (ie. its contents).
Try $doc->getElementById("example1")->nodeValue instead.
That should do it. You may want to add libxml_use_internal_errors(true); so that any errors in the source file won't destroy your page with PHP errors. Also, I would suggest using loadHTMLFile instead of load, as this will be more lenient towards malformed documents.
you can use getElementById:
$a = $doc->getElementById("example1");
var_dump($a); so you will see what you want to echo or put, or something.
You can also make all the names i HTML as example[] end then foreach the example array, so you can get element by id from example array with just one row of code

How to create several pages with dompdf

I am having some trouble with multidimensional array and its value.
What i am looking for is , from my query I am searching teachers name in the array. And after that i want to create a pdf using dompdf. The problem is with looping. I am not able to create a proper loop which will work the way I want it to work. My sample query is
$q11 = "select id from teachers order by teacher ";
$r11 = mysql_query($q11) or die(mysql_error());
while($rows11 = mysql_fetch_array($r11)){
$teacher = $rows11['id'];
$dompdf->"It will start working";
}
Now i know , this code is confusing, but what i want is, it should create dompdf for every teacher in one single pdf file. Like from the query it should fetch teachers, and for each teacher it should create a dompdf page. Currently it is making just one page according to the last value that my query has search.
Please help. It is kinda urgent.
Your loop is working fine. The way you add pages to your PDF is probably wrong. Apparently you are overwriting one page again and again instead of attaching a new one.
EDIT
I've never used dompdf. A quick look into the docs let me think you create something like a HTML markup which then is converted into PDF, did I get this right?
Example code
$html = <<<HTML
<html>
<head>
<style type="text/css">
/* Your document styling goes here */
</style>
</head>
<body>
HTML;
while ( $row = $dbResult->fetch_assoc() ) {
$html .= '<div class="teacherPage">'
. $row['name'] // your teacher document goes here
'</div>';
}
$html .= '</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
If you wonder about the unusual syntax $var = <<<HTML \r\nHTML, that's a heredoc. It's just more comfortable to use heredocs when you have a lot of alien inline code, this can have variables {$varname} and you don't need to worry about quotes. All you need to make sure, is that heredoc closer HTML is in a new line and not indented.
EDIT2
Still not too sure, which library you are using. I find this extension looking pretty good and it's called dompdf, just like you said in your question.
Your latest comment indicates you did not solve your problem so far, so I decided to add some more information to get you to the target.
Disclaimer: I am not going to write functional code and I will not test this, but the following hints will push you into the right direction to get your stuff done.
dompdf is able to read CSS2 and CSS3 properties of your input document.
Each cycle in the while loop above represents one teacher whith each of them getting a own page in the output document.
I put the page into a div container with the class teacherPage. You can fill this container with all the information you want to have displayed for a teacher.
Now all we need to do, is to tell dompdf each teacherPage is a new page. This can be done using #page markup shipped with CSS3
I added an empty css container <style type="text/css"></style> to the example document above, that's where the page styling should go to.
The example CSS
#page teacher {
size: A4 portrait;
margin: 2cm;
}
.teacherPage {
page: teacher;
page-break-after: always;
}
With #page you can define a named page teacher, which can have properties valid for the whole page container.
page-break-after: always will begin a new page after each container
Hope this helps, have fun trying :)
Apparently, no overflow problems and the results are taking more than the permitted margin of the sheet. What you can do is to separate data from the parent in another table and put between them:
<div style="page-break-before: always;"></div>

Categories