I'm using DOMPDF to generate pdf files from html. PDF file is getting generated but there are many blank pages are appearing in between content. Following is my php code.
<?php
$html=file_get_contents("views/testnew.html");
// echo $html;die();
$paper_orientation = 'landscape';
ob_start();
require_once("dompdf/dompdf_config.inc.php");
$pdfcontent = $html;
$dompdf = new DOMPDF();
$dompdf->set_paper('A4', $paper_orientation);
$dompdf->load_html($pdfcontent);
$dompdf->render();
// $pdf = $dompdf->output();
$pdf=$dompdf->stream("my_pdf.pdf", array("Attachment" => 0));
// file_put_contents('Brochure.pdf', $pdf);
?>
Following is my HTML that is I'm writing in PDF file
http://jsfiddle.net/6RmmB/
Since PDF is getting generated I don't think there is any problem with PHP code. Something must be wrong in html, But not able to figure it out what exactly ?
Or I'm missing something in PHP code ?
Content next to "Employer identification number" is appearing after about 8-9 blank pages
This looks to be due to a bug in how dompdf handles paging of table cells. If you remove the outer table, which appears to only exist to supply a border, the page will render better. You may still need to tweak the structure/styling, however, to get exactly what you want.
For example, instead of this:
<table width="100%" border="0" cellpadding="0" cellspacing="0" id="" style="border:1px solid #ccc;color: #000;font-family: Arial,Helvetica,sans-serif;font-size:14px;">
<tr>
<td>
<table width="100%" cellspacing="0" cellpadding="0" border="0" style="border-bottom:1px solid #ccc">
<tr>
<td width="15%" align="left" valign="top" style="border-right:1px solid #ccc;padding:10px;line-height:20px">Form
<img src="/var/www/html/pm5/bodytechniques/working/development/version5/therapist/images/w9-form.JPG" width="83" height="37" style="margin-left:15px" />
<br>(Rev. August 2013)
<br>Department of the Treasury
<br>Internal Revenue Service</td>
<td width="69%" align="left" valign="top" style="border-right:1px solid #ccc;padding:10px;text-align:center;line-height:45px">
<h1 align="center">Request for Taxpayer Identification Number and Certification</h1>
</td>
<td width="16%" align="left" valign="top">
<h3 style="line-height:25px;padding:10px">Give Form to the requester. Do not send to the IRS</h3>
</td>
</tr>
</table>
</td>
</tr>
</table>
Do this:
<div style="border:1px solid #ccc;color: #000;font-family: Arial,Helvetica,sans-serif;font-size:14px;">
<table width="100%" cellspacing="0" cellpadding="0" border="0" style="border-bottom:1px solid #ccc">
<tr>
<td width="15%" align="left" valign="top" style="border-right:1px solid #ccc;padding:10px;line-height:20px">Form
<img src="/var/www/html/pm5/bodytechniques/working/development/version5/therapist/images/w9-form.JPG" width="83" height="37" style="margin-left:15px" />
<br>(Rev. August 2013)
<br>Department of the Treasury
<br>Internal Revenue Service</td>
<td width="69%" align="left" valign="top" style="border-right:1px solid #ccc;padding:10px;text-align:center;line-height:45px">
<h1 align="center">Request for Taxpayer Identification Number and Certification</h1>
</td>
<td width="16%" align="left" valign="top">
<h3 style="line-height:25px;padding:10px">Give Form to the requester. Do not send to the IRS</h3>
</td>
</tr>
</table>
</div>
I have problem when multiple tables doesn't fit to one page.
Solution for this is easy, add <div style="clear: both;"> after table.
Related
I have a html table. Its right aligned. I have a table within it with fields. But when my chrome browser displays the table some portion of table (leftmost) is cropped in the left, ie its leftmost contents are outside of the screen.
The image of how my table looks like is here: https://yadi.sk/i/rR6rm8nuZsfXlA
How to make the right-aligned table to start at the beginning of the screen (on the left side) to see full table contents?
my code is:
<table width=600 border="0" align="right" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td bgcolor="#F8F7F1"><strong>ID</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1">
<?php echo $rows2['a_id']; ?>
</td>
</tr>
<tr>
<td width="18%" bgcolor="#F8F7F1"><strong>Name</strong></td>
<td width="5%" bgcolor="#F8F7F1">:</td>
<td width="77%" bgcolor="#F8F7F1">
<?php echo $rows2['a_name']; ?>
</td>
</tr>
Aligning a table to right has no meaning if your table width is bigger than screen width. You can try adding this to
<style>
#media only screen and (min-width: 600px) {
.mytable {
margin-right: 0px;
margin-left: auto
}
}
</style>
Assign the my table class to the table you want to align right and remove align=right from html.
Just remove the margin on the side and you can also use a percetage value for the auto resize of the table on screen narrowing
It looks like the width of your outer table (600) does not fill the available space on your web page. If you want the table to start at the beginning of the screen, you could try removing the right alignment like this:
<table width=600 border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td bgcolor="#F8F7F1"><strong>ID</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><?php echo $rows2['a_id']; ?></td>
</tr>
<tr>
<td width="18%" bgcolor="#F8F7F1"><strong>Name</strong></td>
<td width="5%" bgcolor="#F8F7F1">:</td>
<td width="77%" bgcolor="#F8F7F1"><?php echo $rows2['a_name']; ?></td>
</tr>
</table>
</td>
</tr>
</table>
I am working on a PHP project which fetches emails and displays them on the screen. In an email it fetches the following html :
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-16">
<style type="text/css">
TD {
font-family: Verdana,Tahoma,Arial, "Sans Serif";
font-size: 10pt;
}
BODY {
font-family: Verdana,Tahoma,Arial, "Sans Serif";
font-size: 10pt;
}
</style>
</head>
<body bgcolor="#eeeeee"><img width="1" height="1" alt="" src="https://trademe.tmcdn.co.nz/images/1pixel.gif?gen=20181128"><table cellspacing="0" cellpadding="0" width="700" bgcolor="white" align="center" style="border-left: 1px #CCCCCC solid; border-right: 1px #CCCCCC solid; border-top: 1px #CCCCCC solid;">
<tr>
<td height="20" colspan="4"> </td>
</tr>
<tr>
<td width="20"></td>
<td><img border="0" alt="Trade Me Logo" width="246" height="48" src="https://trademe.tmcdn.co.nz/images/new-brand-2016/common/tm-logo-2016-246x48-v1.gif?gen=2018112820201"><img src="https://api.trademe.co.nz/tracking/collect?evt=open&tm=email&et=201&mt=75D6A1C7-4DEA-4B06-A3E9-6A12C1B41937&tid=EB71C99D-BEB4-445F-B62B-C172AC5A4CF4"></td>
<td align="center"></td>
<td width="20"></td>
</tr>
<tr>
<td width="20"></td>
<td colspan="2">
<hr size="0" color="#CCCCCC">
<center><small>Security Note: Trade Me will never ask you for your password via email</small></center>
<hr size="0" color="#CCCCCC">
</td>
<td width="20"></td>
</tr>
<tr>
<td width="20"></td>
<td colspan="2" style="padding-left: 10px; padding-top: 10px;"><small>
This is an automated email regarding listing #: 1847238571</small><br><br>
Hi Matthew,
<br><br><div>
A member has asked a question on your listing for "2.4KW 2400W 3KVA 24VDC Pure Sine Wave Power Inverter Solar Caravan Off Grid LCD".
</div><br><table width="100%" cellpadding="3" cellspacing="0" border="0">
<tr>
<td align="center" width="20"><img width="20" height="20" alt="" src="https://trademe.tmcdn.co.nz/images/icon_question.gif"> </td>
<td>what is the warranty like? <small><i>posted by: </i></small> <b>matihegarty</b>
(5 <img align="absmiddle" border="0" src="https://www.trademe.co.nz/images/star.gif">)
<small>8:54 pm, Wed 28 Nov</small></td>
</tr>
</table><br><br><center><b><font size="3">Answer this question</font></b></center><br><br><div>
We recommend you answer all questions on your listings to help buyers make informed decisions. Questions on vehicle listings created in Trade Me Motors will be displayed automatically. For other listings, questions will only be displayed if answered.
</div><br><br>
Happy trading!
<br><br>
The Trade Me team
<br>www.trademe.co.nz<br><br><small>
If you don't wish to receive these emails or prefer plain text email, please update your
email options</small></td>
<td width="20"></td>
</tr>
<tr>
<td colspan="3">
<table cellspacing="0" cellpadding="0" border="0" width="100%" align="center" style="background-color:White;">
<tr>
<td align="center"><br><small><img width="7" height="8" src="https://trademe.tmcdn.co.nz/images/3/common/triangle.gif"> <font color="#666666">advertisement</font></small><br><br></td>
</tr>
</table>
<table cellspacing="0" cellpadding="0" border="0" width="100%" align="center" style="background-color:#9A9A9A;">
<tr>
<td><img style="border-width:0;" src="https://trademe.tmcdn.co.nz/photoserver/adserver/TMI0003-700x70-mates-FA.png?e=" alt="" width="700" height="70"></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
My program does this :
$cleanMessage = new DOMDocument();
#$cleanMessage->loadHTML($this->bodyHTML); //To clean the html code for unclosed td table tags and other
$this->message = $cleanMessage->saveHTML();
But my output is :
��<�!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd"> <�html><�head><�meta
http-equiv="Content-Type" content="text/html; charset=utf-16"><�style
type="text/css"> TD { font-family: Verdana,Tahoma,Arial, "Sans Serif";
font-size: 10pt; } BODY { font-family: Verdana,Tahoma,Arial, "Sans
Serif"; font-size: 10pt; } <�/style><�/head><�body
bgcolor="#eeeeee"><�img width="1" height="1" alt=""
src="https://trademe.tmcdn.co.nz/images/1pixel.gif?gen=20181128"><�table
cellspacing="0" cellpadding="0" width="700" bgcolor="white"
align="center" style="border-left: 1px #CCCCCC solid; border-right:
1px #CCCCCC solid; border-top: 1px #CCCCCC solid;"><�tr><�td
height="20" colspan="4">�<�/td> <�/tr><�tr><�td width="20"><�/td>
<�td><�a
href="https://www.trademe.co.nz/Track.aspx?site=2018112820201&tm=email&et=201&mt=75D6A1C7-4DEA-4B06-A3E9-6A12C1B41937"
style="text-decoration: underline;"><�img border="0" alt="Trade Me
Logo" width="246" height="48"
src="https://trademe.tmcdn.co.nz/images/new-brand-2016/common/tm-logo-2016-246x48-v1.gif?gen=2018112820201"><�/a><�img
src="https://api.trademe.co.nz/tracking/collect?evt=open&tm=email&et=201&mt=75D6A1C7-4DEA-4B06-A3E9-6A12C1B41937&tid=EB71C99D-BEB4-445F-B62B-C172AC5A4CF4"><�/td>
<�td align="center"><�/td> <�td width="20"><�/td> <�/tr><�tr><�td
width="20"><�/td> <�td colspan="2"> <�hr size="0"
color="#CCCCCC"><�center><�small>Security Note: Trade Me will never
ask you for your password via email<�/small><�/center> <�hr size="0"
color="#CCCCCC"><�/td> <�td width="20"><�/td> <�/tr><�tr><�td
width="20"><�/td> <�td colspan="2" style="padding-left: 10px;
padding-top: 10px;"><�small> This is an automated email regarding
listing #: 1847238571<�/small><�br><�br> Hi Matthew, <�br><�br><�div>
A member has asked a question on your listing for "2.4KW 2400W 3KVA
24VDC Pure Sine Wave Power Inverter Solar Caravan Off Grid LCD".
<�/div><�br><�table width="100%" cellpadding="3" cellspacing="0"
border="0"><�tr><�td align="center" width="20"><�img width="20"
height="20" alt=""
src="https://trademe.tmcdn.co.nz/images/icon_question.gif">�<�/td>
<�td>what is the warranty like? ��<�small><�i>posted
by:�<�/i><�/small>�<�b><�a
href="https://www.trademe.co.nz/Members/Listings.aspx?member=4187691&tm=email&et=201&mt=75D6A1C7-4DEA-4B06-A3E9-6A12C1B41937"
style="text-decoration: underline;">matihegarty<�/a><�/b> (<�a
href="https://www.trademe.co.nz/Members/Feedback.aspx?member=4187691&tm=email&et=201&mt=75D6A1C7-4DEA-4B06-A3E9-6A12C1B41937"
style="text-decoration: underline;">5<�/a>�<�a
href="https://www.trademe.co.nz/Members/Feedback.aspx?member=4187691&tm=email&et=201&mt=75D6A1C7-4DEA-4B06-A3E9-6A12C1B41937"><�img
align="absmiddle" border="0"
src="https://www.trademe.co.nz/images/star.gif"><�/a>) ���<�small>8:54
pm, Wed 28 Nov<�/small><�/td>
<�/tr><�/table><�br><�br><�center><�b><�font size="3"><�a
href="https://www.trademe.co.nz/a.asp?id=1847238571&qna=true#qna&tm=email&et=201&mt=75D6A1C7-4DEA-4B06-A3E9-6A12C1B41937"
style="text-decoration: underline;">Answer this
question<�/a><�/font><�/b><�/center><�br><�br><�div> We recommend you
answer all questions on your listings to help buyers make informed
decisions. Questions on vehicle listings created in Trade Me Motors
will be displayed automatically. For other listings, questions will
only be displayed if answered. <�/div><�br><�br> Happy trading!
<�br><�br> The Trade Me team <�br><�a
href="https://www.trademe.co.nz/?tm=email&et=201&mt=75D6A1C7-4DEA-4B06-A3E9-6A12C1B41937"
style="text-decoration:
underline;">www.trademe.co.nz<�/a><�br><�br><�small> If you don't wish
to receive these emails or prefer plain text email, please update your
<�a
href="https://www.trademe.co.nz/MyTradeMe/EmailOptions.aspx?tm=email&et=201&mt=75D6A1C7-4DEA-4B06-A3E9-6A12C1B41937"
style="text-decoration: underline;">email options<�/a><�/small><�/td>
<�td width="20"><�/td> <�/tr><�tr><�td colspan="3"> <�table
cellspacing="0" cellpadding="0" border="0" width="100%" align="center"
style="background-color:White;"><�tr><�td
align="center"><�br><�small><�img width="7" height="8"
src="https://trademe.tmcdn.co.nz/images/3/common/triangle.gif">�<�font
color="#666666">advertisement<�/font><�/small><�br><�br><�/td>
<�/tr><�/table><�table cellspacing="0" cellpadding="0" border="0"
width="100%" align="center"
style="background-color:#9A9A9A;"><�tr><�td><�a
href="https://www.trademe.co.nz/Link.aspx?i=101247"><�img
style="border-width:0;"
src="https://trademe.tmcdn.co.nz/photoserver/adserver/TMI0003-700x70-mates-FA.png?e="
alt="" width="700" height="70"><�/a><�/td> <�/tr><�/table><�/td>
<�/tr><�/table><�/body><�/html>
I tried :
1.
$this->bodyHTML = mb_convert_encoding($this->bodyHTML,'UTF-8','utf-16');
$this->bodyHTML = mb_convert_encoding($this->bodyHTML,'HTML-ENTITIES','UTF-8'); //both lines together
$this->bodyHTML = mb_convert_encoding($this->bodyHTML,'HTML-ENTITIES','UTF-16');
But it still displays garbled or chinese type characters.
What is the right way to display this html correctly?
in your html replace charsetutf-16 with utf-8 or ISO-8859-1 if you see strange characters.
$this->bodyHTML = str_replace("charset=utf-16","charset=utf-8", $this->bodyHTML);
Need help with parsing HTML code by PHP DOM.
This is simple part of huge HTML code:
<table width="100%" border="0" align="center" cellspacing="3" cellpadding="0" bgcolor='#ffffff'>
<tr>
<td align="left" valign="top" width="20%">
<span class="tl">Obchodne meno:</span>
</td>
<td align="left" width="80%">
<table width="100%" border="0">
<tr>
<td width="67%">
<span class='ra'>STORE BUSSINES</span>
</td>
<td width="33%" valign='top'>
<span class='ra'>(od: 02.10.2012)</span>
</td>
</tr>
</table>
</td>
</tr>
</table>
What I need is to get text "STORE BUSINESS". Unfortunately, the only thing I can catch is "Obchodne meno" as a content of first tag, so according to this content I need to get its parent->parent->first sibling->child->child->child->child->content. I have limited experience with parsing html in php so any help will be valuable. Thanks in advance!
Make use of DOMDocument Class and loop through the <span> tags and put them in array.
<?php
$html=<<<XCOE
<table width="100%" border="0" align="center" cellspacing="3" cellpadding="0" bgcolor='#ffffff'>
<tr>
<td align="left" valign="top" width="20%">
<span class="tl">Obchodne meno:</span>
</td>
<td align="left" width="80%">
<table width="100%" border="0">
<tr>
<td width="67%">
<span class='ra'>STORE BUSSINES</span>
</td>
<td width="33%" valign='top'>
<span class='ra'>(od: 02.10.2012)</span>
</td>
</tr>
</table>
</td>
</tr>
</table>
XCOE;
$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('span') as $tag) {
$spanarr[]=$tag->nodeValue;
}
echo $spanarr[1]; //"prints" STORE BUSINESS
I'm in the process of creating a script for our internal customer support system. I want to collect emails from our IMAP inbox (hosted on Gmail) and parse the emails into the database.
What is the best way to clean frames, badly coded tags, and messy formatting so the result is a clean text with minimal formatting?
I'm aware Regular Expressions will most likely play heavily, but I want to know if this functionality exists in another library somewhere that I'm missing.
Edit: More specifically what needs removed:
All inline CSS/Styling, All HTML except simple formatting like Bold, Underline, and Italics.
Here's an email I'm using as a test case, It's a fairly beefy spam email I got from ZoneAlarm, It's got a bit of everything.
<td>
<br>
<br>
<table align="center" bgcolor="#749FD0" border="0" cellpadding="0" cellspacing="0" style="font-family:Arial,Helvetica,sans-serif;font-size:12px;line-height:16px;color:#555555" valign="top" width="700">
<tbody>
<tr>
<td>
<table align="center" border="0" cellpadding="0" cellspacing="0" valign="top" width="680">
<tbody>
<tr>
<td height="10">
<img border="0" height="1" src="http://download.zonealarm.com/bin/images/email/socialguard/spacer.gif" style="display: block; max-width: 2880px;" width="1"></td>
</tr>
</tbody>
</table>
<table align="center" border="0" cellpadding="0" cellspacing="0" valign="top" width="680">
<tbody>
<tr>
<td height="10" width="10">
<img border="0" height="10" src="http://www.zonealarm.com/email/campaigns/2013/2013_06_SummerSale/nw.png" style="display: block; max-width: 2880px;" width="10"></td>
<td bgcolor="#E3ECEC" height="10" width="660">
<img alt="ZoneAlarm by Check Point Software Technologies LTD." border="0" src="http://www.zonealarm.com/email/campaigns/2013/2013_05_MemorialDay/za_transparent.png" width="120" style="display: block; max-width: 2880px;" title="ZoneAlarm by Check Point Software Technologies LTD."></td>
<td align="right" style="font-family:Arial,Helvetica,sans-serif" width="150">
<span style="color:#999999;font-size:12px">Connect with ZoneAlarm</span></td>
<td align="right" valign="middle" width="125">
<img alt="ZoneAlarm Facebook" border="0" src="http://www.zonealarm.com/email/campaigns/2013/2013_05_MemorialDay/facebook.png" width="22" title="ZoneAlarm Facebook" style="max-width: 2880px;"> <img alt="ZoneAlarm Twitter" border="0" width="22" src="http://www.zonealarm.com/email/campaigns/2013/2013_05_MemorialDay/twitter.png" title="ZoneAlarm Twitter" style="max-width: 2880px;"> <img alt="ZoneAlarm YouTube" border="0" src="http://www.zonealarm.com/email/campaigns/2013/2013_05_MemorialDay/youtube.png" title="ZoneAlarm YouTube" height="22" style="max-width: 2880px;"><img border="0" height="15" src="http://download.zonealarm.com/bin/images/email/socialguard/spacer.gif" width="10" style="max-width: 2880px;"></td>
<td bgcolor="#E3ECEC" rowspan="6" align="center" valign="top" width="1">
<img align="right" height="32" src="http://download.zonealarm.com/bin/images/emails/welcome/borderx1.png" width="1" style="max-width: 2880px;">
</td>
</tr>
</tbody>
</table>
<table align="center" border="0" cellpadding="0" cellspacing="0" valign="top" width="680">
<tbody>
<tr>
<td height="10" width="10">
<img border="0" height="10" src="http://www.zonealarm.com/email/campaigns/2013/2013_06_SummerSale/sw.png" style="display: block; max-width: 2880px;" width="10"></td>
<td bgcolor="#E3ECEC" height="10" width="660">
You can use HTML Purifier for this, see: http://htmlpurifier.org/
Trying to create proper PDF document, using PHP and TCPDF.
Can you help me, how can I use writeHTML function to create and center table, in TCPDF?
Tryed with:
$html = '
<div style="margin-left: auto; margin-right: auto; width: 50%">
<table border="1" width="200" align="center"><tr><td><b>Invoice number: '.$this->xInvoiceNumber.'</b></td></tr></table>
<br />
<table border="1" width="200" align="center"><tr><td>'.$this->xClient.'</td></tr></table>
<br />
</div>
... but no luck.
You have to make a table with 3 columns, set the width for every one of them and in the middle one you have to create your table.
<table>
<tr>
<td style="width:25%"></td>
<td style="width:50%"><table><tr><td>Your content</td></tr></table></td>
<td style="width:25%"></td>
</tr>
</table>
I'm not proud of this method but it's working :)
Ok, so I don't know if there is solution for my problem...
However, I did manage to solve it by using writeHTMLCell funcion, ie.
$this->writeHTMLCell(50, 0, 50, 50, 'cellcontent', 'LRTB', 1, 0, true, 'L');
If somebody can find better solution, please reply.
Tnx!
Try this code; it worked for me:
<table style="width:100%">
<tr>
<td style="width:30%">left margin</td>
<td style="width:40%">
<table border="1" style="width:100%">
<thead>
<tr>
<td style="width:100%" colspan="2"></td>
</tr>
<tr>
<td style="width:40%"><b></b></td>
<td style="width:60%"><b></b></td>
</tr>
</thead>
</table>
</td>
<td style="width:30%">rigth margin</td>
</tr>
</table>
Try replacing your opening div tag with this...
<div style="margin:5px auto; width:50%">
Never done anything like this however, this is the code you would need to center a table that is the cross browser compatible
<div style="text:align:center;">
<table style="margin:0px auto" border="1" width="200" align="center">
<tr>
<td><b>Invoice number: </b></td>
</tr>
</table>
<br />
<table style="margin:0px auto"border="1" width="200" align="center">
<tr>
<td>Client</td>
</tr>
</table>
<br />
</div>
If pdfs support css i would advise styling the html elements using css
table{
border:1px solid black;
margin:0px auto;
text-align:center;
width:200px;
}
Hope this helps!