How to Make a div fit A4 page - html

I have a django admin action function that display Transactions and loops through as many as queryset selected that will be rendered in html page, and converts the html to pdf.
I want at the pdf that each transaction object fit in A4, not to have another transaction object in same page. here is code..
def report_pdf(self, request, queryset):
if request.user.is_superuser:
transaction = queryset
else:
transaction = queryset.filter(user=request.user)
template_path = "single-pdf.html"
context = {"transactions": transaction}
template = get_template(template_path)
html = template.render(context)
file = open('test.pdf', "w+b")
pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file,
encoding='utf-8')
file.seek(0)
pdf = file.read()
file.close()
return HttpResponse(pdf, 'application/pdf')
and here is my html
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Report</title>
<style>
</style>
</head>
{% for transaction in transactions %}
<body>
<div class="container">
{% if transaction.complete %}
<table class="tg" >
<thead>
<tr>
<th class="tg-4rlv">Report for Tenant</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-c6of" CHP Reference</span></td>
<td class="tg-c6of">{{transaction.chp_reference}}</td>
</tr>
<tr>
<td class="tg-c6of"> Rent Effective From(dd/mm/yyyy)</td>
<td class="tg-c6of">{{transaction.rent_effective_date}}</td>
</tr>
<tr>
<td class="tg-c6of"> CRA Fortnightly Rates valid for 6 months from</td>
<td class="tg-c6of">{{transaction.cra_rate_from}}</td>
</tr>
<tr>
<td class="tg-l8qj">Market Rent of the property :</td>
<td class="tg-c6of">{{transaction.property_market_rent}}</td>
</tr>
<tr>
<td class="tg-l8qj" >Number of Family Group(s) :</td>
<td class="tg-c6of">{{transaction.number_of_groups}}</td>
</tr>
</div>
</body>
</html>

Related

Parsing and modifying content with Beautiful Soup (bs4)

Goal is to modify existing html's content only.
For example, given current markup:
<html lang="en" op="item">
<head>
<meta name="referrer" content="origin">
<title>The Scientific Case for Two Spaces After a Period (2018)</title>
</head>
<body>
<center>
<table class="fatitem" border="0">
<tr class='athing' id='25581282'>
<td class="title">
<a class="titlelink">The Scientific Case for Two Spaces After a Period (2018)</a>
</td>
</tr>
</table>
</center>
</body>
</html>
Suppose, I want to append "™" string to each word which length is 6.
The result expected:
<html lang="en" op="item">
<head>
<meta name="referrer" content="origin">
<title>The Scientific Case for Two Spaces™ After a Period™ (2018)</title>
</head>
<body>
<center>
<table class="fatitem" border="0">
<tr class='athing' id='25581282'>
<td class="title">
<a class="titlelink">The Scientific Case for Two Spaces™ After a Period™ (2018)</a>
</td>
</tr>
</table>
</center>
</body>
</html>
I'm fairly new to python, and having trouble with this. Because of nested contents, I'm struggling with properly accessing the elements and returning expected outcome.
This is what I have tried so far:
soup = BeautifulSoup(markup, 'html.parser')
new_html = []
for tags in soup.contents:
for tag in tags:
if type(tag) != str:
split_tag = re.split(r"(\W+)", str(tag.string))
for word in split_tag:
if len(word) == 6 and word.isalpha():
word += "™"
tag.string = "".join(split_tag)
else:
str_obj.append(tag)
new_html.append(str(tag))
You can use .find_all(text=True) in combination with .replace_with():
import re
from bs4 import BeautifulSoup
html_doc = """
<html lang="en" op="item">
<head>
<meta name="referrer" content="origin">
<title>The Scientific Case for Two Spaces After a Period (2018)</title>
</head>
<body>
<center>
<table class="fatitem" border="0">
<tr class='athing' id='25581282'>
<td class="title">
<a class="titlelink">The Scientific Case for Two Spaces After a Period (2018)</a>
</td>
</tr>
</table>
</center>
</body>
</html>
"""
soup = BeautifulSoup(html_doc, "html.parser")
for s in soup.find_all(text=True):
new_s = re.sub(r"([a-zA-Z]{6,})", r"\1™", s)
s.replace_with(new_s)
print(soup.prettify())
# to have HTML entities:
# print(soup.prettify(formatter="html"))
Prints:
<html lang="en" op="item">
<head>
<meta content="origin" name="referrer"/>
<title>
The Scientific™ Case for Two Spaces™ After a Period™ (2018)
</title>
</head>
<body>
<center>
<table border="0" class="fatitem">
<tr class="athing" id="25581282">
<td class="title">
<a class="titlelink">
The Scientific™ Case for Two Spaces™ After a Period™ (2018)
</a>
</td>
</tr>
</table>
</center>
</body>
</html>

Invalid location of tag (ul) & (table)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Achievements</title>
</head>
<body>
<h2>수료증 및 수상 내역</h2>
<hr>
<ul>
<h3><li>수료증</li></h3>
<table border="1">
<tr>
<th>과목명</th>
<th>교수명</th>
<th>대학명</th>
</tr>
<tr>
<td>Programming for Everybody(Getting Started with Python)</td>
<td>Charles Severance</td>
<td>University of Michigan</td>
</tr>
</table>
<h3><li>수상내역</li></h3>
<table border="1">
<tr>
<th>대회명</th>
<th>수상일</th>
</tr>
<tr>
<td>The 5th MIRROR Essay Contest</td>
<td>11/23/15</td>
</tr>
</table>
</ul>
<hr>
메인으로
</body>
</html>
I'm using Eclipse Jee Mars btw, and I get warnings saying "Multiple annotations found at this line: Invalid location of tag (h3), Invalid location of tag (h3)" and "Invalid location of (table)". When I open the file through Internet explorer it works fine. What's the problem and how should I fix it?

Thymeleaf Using th:if within a th:block

I am new to thymeleaf and am trying to create an html table where a boolean decides whether the text will be pass or fail in some of the columns.
SmokeTest.passOrFailArray is an array of booleans.
Right now the smokeTest.name is showing up in the column but the passed or failed text is not showing up at all.
Here is my thymeleaf/html code
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Smoke Tests</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<table border="1" style="width:300px">
<tr>
<td>Test Name</td>
<td th:each="testsThatRan : ${testsThatRan}"
th:text="${testsThatRan}">Tests</td>
</tr>
<th:block th:each="smokeTest : ${smokeTests}">
<tr>
<td th:text="${smokeTest.name}">A Smoke Test'</td>
<th:block th:each="smokeTest.passOrFailArray : ${smokeTest.passOrFailArray}">
<td th:if="${smokeTest.passOrFailArray} == true" th:text="Passed"></td>
<td th:if="${smokeTest.passOrFailArray} == false" th:text="failed"></td>
</th:block>
</tr>
</th:block>
</table>
</body>
</html>
Here is the class that im using as a variable in thymeleaf
public testers() throws IOException {
localPath = "/Users/dansbacher14/Documents/workspace/OBI_nightly_test/src/OBI_ci_scripts_tests";
remotePath = "ssh://git#stash.ops.aol.com:2022/obi/obi_ci_scripts.git";
localRepo = new FileRepository(localPath + "/.git");
pathToSmoke = "BPS/GPS/GATHR/SMOKE";
pathToODirectory = "test-results";
git = new Git(localRepo);
}
public static <C> void testClone() throws IOException, InvalidRemoteException, TransportException, GitAPIException
{
Git.cloneRepository().setURI(remotePath).setDirectory(new File(localPath)).call();
}
//____________SETTERS AND GETTERS __________________________________________________________________________________
public void setName(String name)
{
jmxName = name;
}
public String getName()
{
return jmxName;
}
public boolean[] getPassOrFailArray()
{
return passOrFailArray;
}
public String getLocalPath()
{
return localPath;
}
}
This is how the source code is presented by the browser.
<!DOCTYPE HTML>
<html>
<head>
<title>Smoke Tests</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<table border="1" style="width:300px">
<tr>
<td>Test Name</td>
<td>OBI01</td>
<td>DEV</td>
</tr>
<tr>
<td>authAmtTesting.jmx</td>
</tr>
<tr>
<td>authTesting.jmx</td>
</tr>
<tr>
<td>CC_Crypto.jmx</td>
</tr>
<tr>
<td>ci_address.jmx</td>
</tr>
<tr>
<td>ci_cardtype_negative.jmx</td>
</tr>
<tr>
<td>ci_cardtype_positive.jmx</td>
</tr>
<tr>
<td>promoSmokeTst.jmx</td>
</tr>
<tr>
<td>tokenizedPayment.jmx</td>
</tr>
</table>
</body>
</html>
Is it possible to do something like this in thymeleaf? If so how could I make this work? Thanks
This code works
<th:block th:each="pf : ${smokeTest.passOrFailArray}">
<td th:if="${pf} == true" th:text="Passed"></td>
<td th:if="${pf} == false" th:text="failed"></td>
</th:block>
the problem is that i was naming my variable in the each loop incorrectly. The name cannot have a period in it.

Create a table in thymeleaf

I'm new to thymeleaf and am trying to make a simple table using an array and an each loop.
My code looks like this:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Smoke Tests</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<table border="1" style="width:300px">
<tr>
<td>Test Name</td>
</tr>
<tr th:each="smokeTest : ${smokeTests}">
<td>
th:text="${smokeTest.name}">A Smoke Test'
</td>
</tr>
</table>
</body>
</html>
Basically my problem is that I can't run the loop as <td>s within <tr>s. Is there any way that this code could work?
You must put th:text as an attribute of a tag, so
<tr th:each="smokeTest : ${smokeTests}">
<td th:text="${smokeTest.name}">A Smoke Test'</td>
</tr>
should run.
Simple solution which comes to mind first:
<th:block th:each="smokeTest : ${smokeTests}">
<tr>
<td th:text="${smokeTest.name}">A Smoke Test'</td>
</tr>
</th:block>
Details: http://www.thymeleaf.org/whatsnew21.html#bloc
Although, it's late answer.
It's work more specifically, like
<tr th:each="smokeTest : ${smokeTests}">
<td><p th:text="${smokeTest.name}"></p></td>
</tr>

Nokogiri XML to node

I'm reading a local HTML document with Nokogiri like so:
f = File.open(local_xml)
#doc = Nokogiri::XML(f)
f.close
#doc contains a Nokogiri XML object that I can parse using at_css.
I want to modify it using Nokogiri's XML::Node, and I'm absolutely stuck. How do I take this Nokogiri XML document and work with it using node methods?
For example:
#doc.at_css('rates tr').add_next_sibling(element)
returns:
undefined method `add_next_sibling' for nil:NilClass (NoMethodError)
despite the fact that #doc.class is Nokogiri::XML::Document.
For completeness, here is the markup I'm trying to edit.
<html>
<head>
<title>Exchange Rates</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<table class="rates">
<tr>
<td class="up"><div></div></td>
<td class="date">Saturday, Jan 12</td>
<td class="rate up">3.83</td>
</tr>
<tr>
<td class="up"><div></div></td>
<td class="date">Friday, Jan 11</td>
<td class="rate up">3.70</td>
</tr>
<tr>
<td class="down"><div></div></td>
<td class="date">Thursday, Jan 10</td>
<td class="rate down">3.68</td>
</tr>
<tr>
<td class="down"><div></div></td>
<td class="date">Wedensday, Jan 9</td>
<td class="rate down">3.70</td>
</tr>
<tr>
<td class="up"><div></div></td>
<td class="date">Tuesday, Jan 8</td>
<td class="rate up">3.66</td>
</tr>
</table>
</body>
</html>
This is an example how to do what you are trying to do. Starting with f containing a shortened version of the HTML you want to parse:
require 'nokogiri'
f = '
<html>
<head>
<title>Exchange Rates</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<table class="rates">
<tr>
<td class="up"><div></div></td>
<td class="date">Saturday, Jan 12</td>
<td class="rate up">3.83</td>
</tr>
</table>
</body>
</html>
'
doc = Nokogiri::HTML(f)
doc.at('.rates tr').add_next_sibling('<p>foobar</p>')
puts doc.to_html
Your code is incorrectly trying to find the class="rates" parameter for <table>. In CSS we'd use .rates. An alternate way to do it using CSS is table[class="rates"].
Your example didn't define the node you were trying to add to the HTML, so I appended <p>foobar</p>. Nokogiri will let you build a node from scratch and append it, or use markup and add that, or you could find a node from one place in the HTML, remove it, and then insert it somewhere else.
That code outputs:
<!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-8">
<title>Exchange Rates</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<table class="rates">
<tr>
<td class="up"><div></div></td>
<td class="date">Saturday, Jan 12</td>
<td class="rate up">3.83</td>
</tr>
<p>foobar</p>
</table>
</body>
</html>
It's not necessary to use at_css or at_xpath instead of at. Nokogiri senses what type of accessor you're using and handles it. The same applies using xpath or css instead of search. Also, at is equivalent to search('some accessor').first, so it finds the first occurrence of the matching node.
Try to load as HTML instead of XML Nokogiri::HTML(f)
Not getting in much detail on how Nokogiri works, lets say that XML does not have css right? So the method at_css doesn't make sense (maybe it does I dunno). So it should work loading as Html.
Update
Just noticed one thing. You want to do at_css('.rates tr') insteand of at_css('rates tr') because that's how you select a class in css. Maybe it works with XML now.