I put an input text in my form and when I click print it should print in the pdf the text I input in that text field following the table form format. I have a problem on how to catch data from the php file.
PFDF Here is my code:
<?php
$html = <<<MYTABLE
<table border="1" style="width:90%" title="Leave" cellpadding="5px" align="center">
<tr>
<td rowspan=1 width="70" align:"top-left"><center><b>1. Office/Agency</b> </center><br><select name="office_agency"></td>
<td rowspan=1 width="100" colspan="2" align:"top-left"><b>2. ID / Name</b><br> </td>
</tr>
<tr>
<td rowspan=1 align:"top-left"><b>3. Date of Filing:</b><br></td>
<td rowspan=1 align:"top-left"><b>4. Position</b><br></td>
<td rowspan=1 align:"top-left"><b>4. Salary</b><br></td>
..........
MYTABLE;
require('pdftable.inc.php');
mysql_connect('localhost','root','');
mysql_select_db('auth');
$p = new PDFTable();
$p->AddPage();
$p->setfont('times','',10);
$p->Cell(5,-5,' APPLICATION FOR LEAVE');
$p->Ln(3);
$p->htmltable($html);
$p->output();
?>
You need to write the content to the page using Write()
$pdf->Write("content here");
Write Documentation
Related
I'm new to fpdf and I'm having trouble on how can I display the data based on my input field. In myh input field php file I will put the name,date etc and I have a print button where when I clicked It will automatically print what I inputted in that field. Now I want that to be printed in a table form format. Below is the fpdf file:
<?php
$html = <<<MYTABLE
<table border="1" style="width:90%" title="Leave" cellpadding="5px" align="center">
<tr>
<td rowspan=1 width="70" align:"top-left"><center><b>1. Name:</b></center><br><select name="office_agency"></td>
<td rowspan=1 width="100" colspan="2" align:"top-left"><b>2. ID / Date</b><br> </td>
</tr>
<tr>
......
</tr>
</table>
MYTABLE;
require('pdftable.inc.php');
mysql_connect('localhost','root','');
mysql_select_db('auth');
$p = new PDFTable();
$p->AddPage();
$p->setfont('times','',10);
$p->Cell(5,-5,'APPLICATION FOR LEAVE');
$p->Ln(3);
$p->htmltable($html);
$p->output();
?>
There. how can put the code for fetching the inputted data following the html format or is there any way than this? thanks
Here you cannot use the html table for direct display,you can create the table using the col() syntax for more check this link [here]
http://www.fpdf.org/en/script/script14.php
I'am using strip_tags function to fetch only required content but it fetches the whole data from a link
see the example code below i m using to fetch content from a link:
<?php
$a=fopen("http://example.com/","r");
$contents=stream_get_contents($a);
fclose($a);
$contents1=strtolower($contents);
$start='<div id="content">';
$start_pos=strpos($contents1,$start);
$first_trim=substr($contents1,$start_pos);
$stop='</div><!-- content -->';
$stop_pos=strpos($first_trim,$stop);
$second_trim=substr($first_trim,0,$stop_pos+6);
$second_trim = strip_tags($second_trim, '<div><table><tbody><tr><td><a><h2><h4>');
echo "<div>$second_trim</div>";
?>
here is the html code fetched in $second_trim:
<div><div id="content">
<div id="issuedescription"></div>
<h2 class="wsite-content-title" style="text-align:center;">download content<br /><font color="#f30519">table of content</font><br /> <font color="#f80117"> content </font></h2>
<h2>table of contents</h2>
<h4 class="tocsectiontitle">editorial</h4>
<h2 class="wsite-content-title" style="text-align:left;">technical note</h2>
<table class="tocarticle" width="100%">
<tr valign="top">
<td class="toctitle" width="95%" align="left">where are we at and where are we heading to? </td>
<td class="tocgalleys" width="5%" align="left">
pdf
</td>
</tr>
<tr>
<td class="tocauthors" width="95%" align="left">
sergio eduardo de paiva gonã§alves </td>
<td class="tocpages" width="5%" align="left">1-2</td>
</tr>
</table>
<div class="separator"></div>
h4 class="tocsectiontitle">some text here</h4>
<table class="tocarticle" width="100%">
<tr valign="top">
<td class="toctitle" width="95%" align="left">some text here</td>
<td class="tocgalleys" width="5%" align="left">
pdf
</td>
</tr>
<tr>
<td class="tocauthors" width="95%" align="left">
some text here, some text here, some text here, some text here, some text here, some text here </td>
<td class="tocpages" width="5%" align="left">3-10</td>
</tr>
</table>
<a target="_blank" rel="license" href="http://example.com/">
</a>
some text here<a rel="license" target="_blank" href="http://example.com/">example</a>.
</div></div>
Now my problem is i want to fetch a particular tag only, from the whole content like 2nd anchor from two of given below using strip_tag function
pdf
some text here
and 2nd header tag from two of given below:
<h2 class="wsite-content-title" style="text-align:center;">download content<br /><font color="#f30519">table of content</font><br /> <font color="#f80117"> content </font></h2>
<h2>table of contents</h2>
but strip tag function is either fetching all of them or none of them , So how can i make them identify to fetch the tag I want instead of fetching all the similar tags.If their is any better way to do this please share your ideas here !!
A regexp can do such a thing:
function handle_link($data) {
list($link, $attributes, $content) = $data;
$classes = preg_match('#class=[\'"]([^\'"]+)[\'"]#', $attributes, $match) ? preg_split('#\s+#', $match[1]) : array();
// If the link has the "file" class
if(in_array('file', $classes)) {
return $content; // only the internal content (like strip_tags would do)
// or you can return a new link:
// return '' . $content . '';
} else {
return $link; // all the link not filtered
}
}
$second_trim = strip_tags($second_trim, '<div><table><tbody><tr><td><h2><h4>');
$second_trim = preg_replace_callback('#<a([^>]*)>(.+)</a>#U', 'handle_link', $second_trim);
I am fetch data from table insdie php script and displaying in table. This php script is called by button click event.
Here is the php code which does this:
while ($row = #mysqli_fetch_array($result))
{
echo '<div class = "c2">
<table class="table table-condensed">
<tbody>
<tr class="active">
<td class="active"></td>
<td class="active">'.$row['date'].'</td>
<td class="success">'.$row['time'].'</td>
<td class="warning">'.$row['username'].'</td>
<td class="danger">'.$row['user_follower_count'].'</td>
<td class="info">'.$constant.'</td>
<td class="active">'.$row['tweet_text'].'</td>
<td class="success">...dfd</td>
</tr>
</tbody>
</div>
';
}
However the content display looks messed.
I have used bootstrap css here
Like
How can I display so that all rows appears with same width in proper manner?
Content is displayed when button is clicked.
Try this:
echo '<div class="c2"><table class="table table-condensed"><tbody>';
while ($row = #mysqli_fetch_array($result))
{
echo '<tr class="active">
<td class="active"></td>
<td class="active">'.$row['date'].'</td>
<td class="success">'.$row['time'].'</td>
<td class="warning">'.$row['username'].'</td>
<td class="danger">'.$row['user_follower_count'].'</td>
<td class="info">'.$constant.'</td>
<td class="active">'.$row['tweet_text'].'</td>
<td class="success">...dfd</td>
</tr>';
}
echo '</tbody></table></div>';
I am trying to pull each td element from the html table below and import each element into its own cell in a CSV file.
Here are the two html tables:
<table width="100%" border="0" cellspacing="1" cellpadding="0" bgcolor="#006699">
<tr align="center" class="tableRow1Font">
<td width="7%">WAITLIST</td>
<td width="5%">91630</td>
<td width="11%">
ACCY 2001
</td>
<td width="5%">10</td>
<td width="16%">Intro Financial Accounting</td>
<td width="6%">3.00</td>
<td width="8%"> Zou, Y</td>
<td width="8%"><A HREF="http://www.gwu.edu/~map/building.cfm?BLDG=DUQUES" target="_blank"
>DUQUES</a> 251</td>
<td width="13%">TR<br>09:35AM - 10:50AM</td>
<td width="14%">
01/13/14 - 04/28/14
</td>
<td width="7%">
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="1" cellpadding="0" bgcolor="#006699">
<tr align="center" class="tableRow2Font">
<td width="7%">WAITLIST</td>
<td width="5%">90003</td>
<td width="11%">
ACCY 2001
</td>
<td width="5%">11</td>
<td width="16%">Intro Financial Accounting</td>
<td width="6%">3.00</td>
<td width="8%"> Zou, Y</td>
<td width="8%"><A HREF="http://www.gwu.edu/~map/building.cfm?BLDG=DUQUES" target="_blank"
>DUQUES</a> 254</td>
<td width="13%">TR<br>11:10AM - 12:25PM</td>
<td width="14%">
1/13/14 - 04/28/14
</td>
<td width="7%">
</td>
</tr>
</table>
I have written code that goes through the tables and pulls the td elements:
foreach($html->find('tr[align=center] td') as $e)
$str .= strip_tags($e->innertext) . ', ';
echo $str;
So how can I extract these elements into a CSV file? In Excel I want it to look like this with each td element in its own cell, starting a new row for each html table:
WAITLIST 91630 ACCY 2001 10 Intro Financial Accounting 3.00 Zou, Y DUQUES 251 TR
WAITLIST 90003 ACCY 2001 11 Intro Financial Accounting 3.00 Zou, Y DUQUES 251 TR
There is a library exist for this. Goto http://phpexcel.codeplex.com/. Download the zip file and in example you would find 17html.php try this code. I hope this will help.
CSV means Comma Separated Values. Thus, as you echo out the data (after running it through your function to strip the <td> tags), put commas in between each piece of data (cell), and a new line where you want the next line to start.
So to use your example above, it should look like this:
WAITLIST,91630,ACCY,2001,10,Intro Financial Accounting,3.00,Zou,Y,DUQUES,251,TR
WAITLIST,90003,ACCY,2001,11,Intro Financial Accounting,3.00,Zou,Y,DUQUES,2,
Keep in mind that when you echo this, you shouldn't have any other html tags or anything.
I have something along the following lines in terms of HTML. I would like to extract the various contents of the table cells, however I discovered that there are some embedded divs occasionally in the cells and perhaps other oddities that I'm not sure of yet:
<p align="center">
<img src="some_image.gif" alt="Some Title">
</p>
<TABLE WIDTH=500 BORDER=1 class=textwhite ALIGN=center CELLPADDING=0 CELLSPACING=0>
<TR>
<TD colspan=4 ALIGN=center><b>Title</b></TD>
</TR>
<TR>
<TD ALIGN=center>Title</TD>
<TD ALIGN=center>date</TD>
<TD ALIGN=center>value</TD>
<TD ALIGN=center>value</TD>
</TR><TR>
<TD ALIGN=center>Title2</TD>
<TD ALIGN=center></TD>
<TD ALIGN=center><div class=redtext>----</div></TD>
<TD> </TD>
</TR><TR>
<TD ALIGN=center>Title3</TD>
<TD ALIGN=center><div class=yellowtext>value</div></TD>
<TD ALIGN=center><div class=redtext>value</div></TD>
<TD ALIGN=center>value<SUP>6</SUP></TD>
</TR><TR>
<TD ALIGN=center>Title4</TD>
<TD ALIGN=center><div class=bluetext>value</div></TD>
<TD ALIGN=center><div class=redtext>value</div></TD>
<TD> </TD>
</TR></TABLE>
<blockquote>
<p class="textstyle">
Text.
</p>
</blockquote>
My first impulse was to extract ALL element texts and just programmatically slice it up. I would watch for Title1, Title2, etc. to know when a row starts and then if a "----" is found meaning no value, just skip this row and move on. However, I realized that there is probably a better way of handling this with xpath directly.
How could this be solved with xpath so as to essentially give each cell's final child text content vs having to walk into each div if it exists? Or is there a more xpath like way to approach this?
Obviously I'm attempting to have the most flexible solution that will not be brittle if other unexpected elements crop up, even though they are unlikely.
The provided text isn't well-formed XML document, therefore XPath isn't applicable.
If you correct and covert it to a well-formed xml document as the one below, an expression like this might be useful:
/*/TABLE//TD//text()
or even:
//TABLE//TD//text()
Here is a wellformed XML document, constructed from the provided HTML:
<html>
<p align="center">
<img src="some_image.gif" alt="Some Title"/>
</p>
<TABLE WIDTH="500" BORDER="1" class="textwhite" ALIGN="center" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD colspan="4" ALIGN="center">
<b>Title</b>
</TD>
</TR>
<TR>
<TD ALIGN="center">Title</TD>
<TD ALIGN="center">date</TD>
<TD ALIGN="center">value</TD>
<TD ALIGN="center">value</TD>
</TR>
<TR>
<TD ALIGN="center">Title2</TD>
<TD ALIGN="center"></TD>
<TD ALIGN="center">
<div class="redtext">----</div>
</TD>
<TD> </TD>
</TR>
<TR>
<TD ALIGN="center">Title3</TD>
<TD ALIGN="center">
<div class="yellowtext">value</div>
</TD>
<TD ALIGN="center">
<div class="redtext">value</div>
</TD>
<TD ALIGN="center">value
<SUP>6</SUP>
</TD>
</TR>
<TR>
<TD ALIGN="center">Title4</TD>
<TD ALIGN="center">
<div class="bluetext">value</div>
</TD>
<TD ALIGN="center">
<div class="redtext">value</div>
</TD>
<TD> </TD>
</TR>
</TABLE>
<blockquote>
<p class="textstyle"> Text. </p>
</blockquote>
</html>
So maybe you don't want to walk the divs, but here is my solution using lxml, which I highly recommend:
import re
from cStringIO import StringIO
from lxml import etree
def getTable(html, table_xpath, rows_xpath, cells_xpath):
"""Get a table on a webpage"""
parser = etree.HTMLParser()
# Build document tree and get table
root = etree.parse(StringIO(html), parser)
table = root.find(table_xpath)
if table == None:
print 'No table.'
return []
rows = table.findall(rows_xpath)
document = []
def cleanText(text):
"""Clean up text by replacing line breaks and tabs. """
return re.sub(r'[\r\n\t]+','',str(text).strip())
# iterate over the table rows and collect text from each cell.
for r in rows:
cells = r.findall(cells_xpath)
rowdata = []
for c in cells:
text = ''
it = c.itertext()
for i in it:
text += cleanText(i) + ' '
rowdata.append(text)
document.append(rowdata)
return document
html = """
<html><head><title></title></head><body>
<p align="center">
<img src="some_image.gif" alt="Some Title">
</p>
<TABLE WIDTH=500 BORDER=1 class=textwhite ALIGN=center CELLPADDING=0 CELLSPACING=0>
<TR>
<TD colspan=4 ALIGN=center><b>Title</b></TD>
</TR>
<TR>
<TD ALIGN=center>Title</TD>
<TD ALIGN=center>date</TD>
<TD ALIGN=center>value</TD>
<TD ALIGN=center>value</TD>
</TR><TR>
<TD ALIGN=center>Title2</TD>
<TD ALIGN=center></TD>
<TD ALIGN=center><div class=redtext>----</div></TD>
<TD> </TD>
</TR><TR>
<TD ALIGN=center>Title3</TD>
<TD ALIGN=center><div class=yellowtext>value</div></TD>
<TD ALIGN=center><div class=redtext>value</div></TD>
<TD ALIGN=center>value<SUP>6</SUP></TD>
</TR><TR>
<TD ALIGN=center>Title4</TD>
<TD ALIGN=center><div class=bluetext>value</div></TD>
<TD ALIGN=center><div class=redtext>value</div></TD>
<TD> </TD>
</TR></TABLE>
</body>
</html>
"""
tp = "//table[#width='500']"
rt = "tr"
cp = "td[#align='center']"
doc = getTable(html, tp, rt, cp)
print repr(doc)
I believe that your program is going to run into many problems as the input data is manipulated -- what if the case of 'title' changes, or there is a typo?
It's not really possible to make a rigorous solution to scraping someone else's website, as they can at no notice completely change everything. Better is normally to write tolerant and flexible code that at least tries to verify that its output is sane. In this case it's probably best to iterate over the results of '//table/tr', then inside this loop, process the td elements:
import lxml.etree
tree = lxml.etree.fromstring("<table><tr><td>test</td></tr><tr><td><div>test2</div></td></tr></table>")
stringify = lambda x : "".join(x.xpath(".//text()"))
for x in tree.xpath("//table/tr"):
print "New row"
for y in x.xpath("td"):
print stringify(y)
Output:
New row
test
New row
test2
The following code will, however, get the list you ask for:
print map(stringify, tree.xpath("//table/tr/td"))
Output:
['test', 'test2']
This will find all text elements which are at all descended from a td which is a direct descendant of a tr which is in turn a direct descendant of a table.
(Simply asking for all text() elements will create some funny bugs when run on HTML which contains "<td>Foo <b>bar</b></td>" or similar.)