Better way to generate PDF from python3 - pdf

My application is using PyQt4 and Python3 (I dont know how to backport it to python2.7). I have a need to export PDF and also print to paper. My search for reporting tool ended nowhere, since ReportLab nor POD support python3. Somehow, I managed to create PDF files with this kind of...hassle.
tekstStampa = (str("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%sIme stranke: %s%sBroj predmeta: %s%sSudski broj: %s%s%sPredmet otvoren: %s%sDatum rasprave: %s u %s sati%s%sStatus stranke: %s%sStatus predmeta: %s%sTip postupka: %s%sVrednost spora: %s dinara.%s%sSud: %s%sSudska jedinica: %s%sSudija: %s%s%sNapomena: %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s" %
(zaglavlje1,'<br>',zaglavlje2,'<br>',zaglavlje3,'<br>',zaglavlje4,'<br>','<br>',
linija,'<br>','<br>','<br>',prazno50,zaglavlje5,brPredmeta,'<br>',prazno51,'<font size="1">datum Å¡tampanja: ',datumStampe,'</font><br>','<br>',
tempime, '<br>',brPredmeta,'<br>', tempsudskibr,'<br>','<br>',
tempdatumtuzba, '<br>', tempdatumRas,tempvreme,'<br>','<br>',
tempstatusstr,'<br>',tempstatusPredmeta,'<br>',temptip,'<br>',tempvrednost,'<br>','<br>',
tempsud,'<br>',tempsudska,'<br>',tempsudija,'<br>','<br>',
'<br>',tempnapomena,'<br>','<br>',
linija, '<br>', '<br>',futer1,'<br>',
futer2,'<br>',
futer3,'<br>',
futer4,'<br>',
futer5,'<br>',)))
self.ui.textStampa.setHtml(str(tekstStampa).replace('\\n','\r'))
As you can see, it's pretty messy, but the output looks acceptable. Not quite good, only acceptable. Example
Now, I need to generate something like this. Columns should be placed in fixed positions, column width is pre-determined. Rows are generated from list, which is queried from database. Quering is fine, I can get the data, but PAGE FORMATTING is killing me.
I have tried to use HTML tags (namely PRE for preformatted text, but it looks ugly) but I wasn't able to accomplish what I wanted.
Thank you in advance.

for tuple in sviaktivni: # extract variables from list of tuples
tempid,ime,brPredmeta,statusStr,sudskiBr ,sudija ,datumRasprave, vreme, zaduzen,datumZaduzenja = tuple
brojac +=1
tekst = ('<html><head><title></title>' #variable which contains HTML tables
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>'
'<style></style>'
'</head>'
'<body>'
'<table align ="center" border="0" width="100%" style="table-layout:fixed">'
'<tr height="10%">'
'<td align="left" width="3%">'+str(brojac)+' </td>'
'<td align="left" width="15%">'+ime+' </td>'
'<td align="left" width="10%"> '+brPredmeta+' </td>'
'<td align="left" width="10%"> '+statusStr+' </td>'
'<td align="left" width="10%"> '+sudskiBr+' </td>'
'<td align="left" width="18%"> '+sudija+' </td>'
'<td align="left" width="15%"> '+zaduzen+' </td>'
'<td align="left"> '+datumZaduzenja+' </td>'
'</tr>'
'</table>'
'</body>'
'</html>')
self.ui.textStampa.append(tekst) #append with every iteration

Related

grabTextFrom finds anticipated string that assertion using Locator::contains cannot

I have an interesting case with the following HTML and a Codeception Acceptance test using PhpBrowser, which I've not been able to find a similar issue for on StackOverflow.
I am looking to assert that the content of a P tag (as part of the only table on the page) contains an anticipated phrase.
// representative code
$anticipatedValue = "updated text description";
$xpath = "//table/tbody/tr[position() = 1]/td[position() = 2]/descendant::p";
$val = $I->grabTextFrom($xpath); // this equals the value of $anticipatedValue;
echo "the value is[" .$val."][".$anticipatedValue."]"; // these match perfectly, no trimming needed
$I->see(Locator::contains($xpath,$anticipatedValue)); //this fails
$I->see(Locator::contains($xpath,$val)); //so does this
The fail HTML output does not display anything out of the ordinary. I am hoping someone with more experience of XPath in PhpBrowser can point out what I am missing.
The HTML portion I am looking at is below. This is a legacy application I am adding tests to, so the HTML is at present, as you can see, not optimal.
<table id="calendar" class="unitable" cellspacing=0 cellpadding=0>
<thead>
<tr>
<th>Time</th>
<th></th>
</tr>
</thead>
<tbody>
<tr class="past"><td>10 Dec 00:00</td>
<td class="entry" rowspan=2>
<div class="time">Tue 10 09:00 - Tue 10 17:00</div>
<div class="stage">new stage B</div>
<p class="description">updated text description</p>
</td>
</tr>
<tr class="past">
<td>10 Dec 12:00</td>
</tr>
<tr class="past">
<td>11 Dec 00:00</td>
<td class="entry" rowspan=1>
<div class="time">Wed 11 09:00 - Wed 11 11:30</div>
<div class="stage">new stage C</div>
<p class="description">this is new stage C</p>
</td>
</tr>
<tr class="past">
<td>11 Dec 12:00</td>
<td></td>
</tr>
<tr class="past">
<td>12 Dec 00:00</td>
<td></td>
</tr>
<tr class="past">
<td>12 Dec 12:00</td>
<td class="entry" rowspan=1>
<div class="time">Thu 12 13:30 - Thu 12 17:00</div>
<div class="stage">new stage D</div>
<p class="description">this is new stage D</p>
</td>
</tr>
</tbody>
<tfoot></tfoot>
</table>
and the failure report reads:
Failed asserting that on page ...
{short snippet of upper page HTML - not from the area under scrutiny, followed by}
[Content too long to display. See complete response in '/var/www/public/vhosts/system/tests/_output/' directory]
--> contains "//table/tbody/tr[position() = 1]/td[position() = 2]/descendant::p[contains(., 'updated text description')]".
Many thanks for your consideration of this issue.
Your problem is that you pass result of Locator::contains to $I->see() method,
see expects to get a string as a first parameter and it is looking for that exact text in HTML.
Start with
$I->see('updated text description');
If you want to check if the text is displayed in specific location, pass XPath expression as a second parameter:
$I->see('updated text description', '//table/tbody/tr[position() = 1]/td[position() = 2]/descendant::p');
as documented at https://codeception.com/docs/modules/PhpBrowser#see

How to create table for my perl output using perl cgi?

Here is my code and output i was unable to create the different row for my each line using perl?
Here is my code:
use strict;
use warnings;
use CGI;
open(my $file1,"as.txt");
my $firstline=<$file1>;
$firstline=~s/.*=//g;
my #words=split /,/,$firstline;
my $finalline=join("\n",#words);
close $file1;
print "Content-type:text/html\n\n"
print<<"EOF";
<html><body>
<table style="width:100%">
<tr>
<th>UserName</th>
<th>Access</th>
</tr>
<tr>
<td>$finalline</td>
<td>
<input type="checkbox" value="check2" mulitple checked>Read
<input type="checkbox" value="check2" mulitple>Write
<input type="checkbox" value="check2" mulitple>Owner
</td>
</tr>
</table></body></html>
EOF
MY OUTPUT FOR PERL (I.E $finalline)
sankar
morien3
i got the following table as my output in table format:
UserName Access
sankar morien3 Read Write Owner
Expected output:
UserName Access
sankar Read Write Owner
morien3 Read Write Owner
Input file:(i.e as.txt)
cskTeam = sankar, mobrien3
[csk:/]
* = r
#cskTeam = rw
You must know about how HTML layout will work.
Even your code won't give your expected result in terminal.
In html \n not make sense, <br> it is for new line in html. But this logic also won't work in your code.
You are joining the array with \n, then printing the data it will execute the comment line by line what you have mentioned.
First you have printing the $finalline variable so it result is
sankar \n morien3
\n will not consider in html. Storing in one cell as per <td>
Then you are creating the another cell which holds the permission detail.
Finally your code should be as follows.
#!/usr/bin/perl
use warnings;
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
print "Content-Type: text/html \n\n";
open my $file1,"<","as.txt";
my $firstline=<$file1>;
$firstline=~s/.*=//g;
my #words=split /,/,$firstline;
close $file1;
print<<"EOF";
<html><body>
<table style="width:50%; ">
<tr>
<th style="text-align:left">UserName</th>
<th style="text-align:left">Access</th>
</tr>
EOF
foreach (#words)
{
print <<EOF;
<tr>
<td>$_</td>
<td>
<input type="checkbox" value="check2" mulitple checked>Read
<input type="checkbox" value="check2" mulitple>Write
<input type="checkbox" value="check2" mulitple>Owner
</td>
</tr>
EOF
}
print <<EOF;
</table></body></html>
EOF

Response.Write Horizontal Table from SQL Query using Dataview

Still need help, no answer that has worked yet. Please jump in if you have any ideas.
I have been trying to get my Response.Write to go Horizontal, however I cannot seem to figure it out.
This is what I currently have (it all sits within a table) and loads from an sql database query using Dataview to get the vehicles I want to display:
Response.Write("<tr><td width='200px' colspan='3'><a href='car.aspx?invno=" & invno & "'><image runat='server' src='" + imageurl + "'></image></a></td></tr>")
Response.Write("<tr><td width='5px'>" + year + "</td><td width='10px'>" + make + "</td><td width='100px'>" + model + "</td></tr>")
Response.Write("<tr><td width='200px' colspan='3'>" + mileage + "</td></tr>")
However it is Currently giving me this Picture 1
But what I really want is this Picture 2
Any help would be greatly appreciated
Update
Ok so I am editing this whole post, as it was overly complicated with what I want. So I am breaking it down to its simplest:
</head>
<body>
<form id="form1" runat="server">
<asp:SqlDataSource runat="server" id="sqldata" ConnectionString="<%$ ConnectionStrings:cars %>" SelectCommand="SELECT TOP 3 ORDER BY NEWID() )">
</asp:SqlDataSource>
<asp:Panel ID="Panel1" runat="server">
<div style="font-size: 10px; font-family: 'arial';">
<center>
<table id="list" border="0" cellpadding="2" cellspacing="0" width="200px">
<%
Dim dv As Data.DataView = CType(sqldata.Select(DataSourceSelectArguments.Empty), Data.DataView)
For Each dr As Data.DataRow In dv.Table.Rows
Dim make As String = dr("make")
Response.Write("<tr><td>" + make + "</td></tr>")
Next
%>
</table>
</center>
</div>
</asp:Panel>
</form>
</body>
</html>
So with the code above.. I get a vertical list
Ford
Chevy
Toyota
But what I want is a Horizontal list
Ford Chevy Toyota
Create a single row with 3 different columns to achieve your desired result
Response.Write("<tr><td width='200px' colspan='3'><a href='car.aspx?invno=" & invno & "'><image runat='server' src='" + imageurl + "'></image></a></td>")
Response.Write("<td width='5px'>" + year + "</td><td width='10px'>" + make + "</td><td width='100px'>" + model + "</td>")
Response.Write("<td width='200px' colspan='3'>" + mileage + "</td></tr>")

SQL select statement with AND and OR operators issue

I'm fetching records from the SQL Server 2000 database using Classic ASP. This is what the code I have used.
ASP Code:
<form name="search" method="post" onsubmit="return attention();">
<table width="60%" border="0" cellpadding="2" cellspacing="0" style="margin:0 auto">
<tr>
<td colspan="2"><h1 style="color:#003399;">Search for Certificate of Analysis</h1></td>
</tr>
<tr>
<td valign="bottom">Product number <sup style="color:#EE0000;font-size:1.3em">*</sup></td>
<td valign="bottom">Lot number</td>
</tr>
<tr>
<td width="30%" valign="top">
<input type="text" name="input1" size="20"/>
</td>
<td valign="top">
<input type="text" size="20" name="input2"/>
<input style="background-color:#ffffff;border:0px;cursor:pointer" size="10" type="submit" name="sub" value=">>" />
</td>
</tr>
</table>
<%
if request.Form("sub") <> "" then
Dim fname, lname
fname= request.Form("input1")
lname= request.Form("input2")
session("n") = fname & lname
sql = "Select * from search where cat_no_batch_no LIKE '"&request.Form("input1")&"%' OR cat_no_batch_no='"&session("n")&"'"
rs.open sql, con, 1, 2
if rs.eof then
response.write("Please provide a valid Cat No.")
else
do while not rs.eof
%>
<table border="1" cellpadding="5" cellspacing="0" width="60%" style="margin:0 auto; border-collapse:collapse;border-color:#999999">
<tr>
<td width="50%"><%=rs("cat_no_batch_no")%></td>
<td width="10%">Click to open <a target="_blank" href="http://localhost/search1/<%=rs("pdf_path")%>"><%=rs("cat_no_batch_no")%></a></td>
</tr>
</table>
<%
rs.movenext
loop
end if
rs.close
end if
%>
</form>
Above the LIKE statement works as expected and displays multiple records which contains similar Cat No..
Issue rises when I input a Cat No in first Input box and Lot Number in another. My desired output should have been just a single record to display as I have given Cat No. and Lot Number , but it shows multiple records.
I have provided the images to be more clear.
In this image below I have put 6106021 as the product number so it displays two entries.
In this image I have put 6106021 as the product number and 218111 as the Lot Number, it shows two entries instead of 1. This is what the issue, it should show one entry as Lot number are unique while cat number can be the same.
Read your question to yourself and think about what you are saying:
You state "My desired output should have been just a single record to display as I have given Cat No. and Lot Number"
But in your SQL you write where cat_no_batch_no LIKE '"&request.Form("input1")&"%' OR cat_no_batch_no='"&session("n")&"'
The key words are AND vs. OR
In your mind you are thinking both conditions must be true, but your query says otherwise. If you want both conditions to be true then you must use the AND operator.
Naoki is on to something - you are going to have to modify your SQL depending upon which criteria are specified.
I guess you can write two separate SQL queries, according as the "Lot number" is provided or not.
Your code may goes like below:
if lname = "" then
sql = "Select * from search where cat_no_batch_no LIKE '"&request.Form("input1")&"%'"
else
sql = "Select * from search where cat_no_batch_no LIKE '"&session("n")&"%'"
end if
I hope this could help

SQL Join in CodeIgniter

I have one file, then for every file there are multiple comments about that file. I was able to successfully join the two tables, comments and files, however when I try to view the page where it displays the file and its comments, the page displays the name of file n times (n corresponds to the number of records of comments of that certain file).
How will I be able to display just once the file name as well as displaying the comments?
<table width="40%" align="center">
<?php foreach($rows as $file):?>
<tr>
<td width="70%" id="title"><?php echo $file->name; ?></td>
</tr>
<?php endforeach;?>
</table>
<table width="40%" align="center" frame="above" style="margin-top:10px; border-top-width:3px; border-top-color:#666666">
<?php foreach($rows as $file):?>
<tr>
<td width="15%"></td>
<td width="45%" id="name">FILESIZE:</td>
<td width="40%" id="txt3"><?php echo $file->size; ?></td>
</tr>
<tr>
<td></td>
<td id="name">DATE UPLOADED:</td>
<td id="txt3"><?php echo $file->dateUploaded;?></td>
</tr>
<?php endforeach;?>
</table>
<!--COMMENTS -->
<table align="center" frame="above" style="margin-top:10px; border-top-width:3px; border-top-color:#666666" width="40%">
<tr>
<td><h3>Comments</h3></td>
</tr>
<tr><?php foreach($rows as $comment):?>
<td><?php echo $comment->comm;?></td>
</tr><?php endforeach;?>
</table>
You can use
GROUP_CONCAT()
in your join.
SELECT person.id AS id, name, GROUP_CONCAT(role.role SEPARATOR ',') AS roles
FROM person
LEFT OUTER JOIN person_role ON person.id=person_role.person_id
LEFT OUTER JOIN role ON person_role.role_id=role.id
GROUP BY id
ORDER BY id;
is an example from a tutorial at
http://dev-loki.blogspot.com/2008/01/aggregate-join-results-with-mysql-using.html
can't give much better without seeing your schema.
EDIT: trying anyway.
SELECT files.*, GROUP_CONCAT(comments.comment SEPARATOR ',') as comments_list
FROM files
LEFT JOIN comments on files.id = comments.id
GROUP BY id
ORDER BY id
Adding onto orbit, in codeigniter if your using activerecord it would be:
(Sorry, I couldn't format code in a comment)
$this->db->select('person.id AS id, name, GROUP_CONCAT(role.role SEPARATOR ',') AS roles',false);
$this->db->from('person');
$this->db->join('person_role', 'person.id = person_role.person_id', 'left outer');
$this->db->join('role', 'person_role.role_id = role.id', 'left outer');
$this->db->group_by('id');
$this->db->order_by('id'):
$this->db->get();
well it looks like you're echoing the filename for each row that is returned
your rows are getting returned like
name | size | comment1
name | size | comment2
so just don't do foreach ($rows as $file) that will print out the filename each time, just change
<?php foreach($rows as $file):?>
<tr>
<td width="70%" id="title"><?php echo $file->name; ?></td>
</tr>
<?php endforeach;?>
to
<tr>
<td width="70%" id="title"><?php echo $rows[0]->name; ?></td>
</tr>