i am trying to generate pdf using xhtml2pdf in django for other than english but it shows black square boxes and unusual texts.
i am trying to render nepali text in my pdf but i m getting this issue, can anyone help me to solve this issue ,,
import imp
from io import StringIO
# from weasyprint import HTML
from django.template.loader import render_to_string
import tempfile
from django.http import HttpResponse
from django.conf import settings
import datetime
# A stream implementation using an in-memory bytes buffer
# It inherits BufferIOBase
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa # a html2pdf converter
def render_to_pdf(template_src, context_dict={}):
"""
This method will converts the template file into pdf
#params template_src is the template file to be converted
#params context_dict is the dictionary containing all the data written into the template
"""
template = get_template(template_src)
context_dict.update(
{
"invoice_title": context_dict["invoice_title"]
+ str(datetime.datetime.now())
+ ".pdf"
}
)
# this will render the html template and parse the data into the template
html = template.render(context_dict)
# result = StringIO()
response = HttpResponse(content_type="application/pdf")
response["Content-Disposition"] = (
"inline;attachment; filename="
+ context_dict["invoice_title"]
+ str(datetime.datetime.now())
+ ".pdf"
)
response["Content-Transfer-Encoding"] = "utf8"
# part will create the pdf.
# pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
pisa_status = pisa.pisaDocument(html.encode("UTF-8"), response)
# pisa_status= pisa.CreatePDF(
# html, dest=response
# )
if pisa_status.err:
return HttpResponse("We had some errors <pre>" + html + "</pre>")
return response
html template:
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Purchase Invoice</title>
<style>
.heading {
font-size: 12px;
font-display: bold;
text-align: center;
padding-top: 50px;
}
.subheading {
font-size: 12px;
text-align: center;
}
.col-12 {
width: 100%;
float: left;
}
.col-3 {
width: 25%;
float: left;
}
.col-6 {
width: 50%;
float: left;
}
.textalignright {
text-align: right;
font-size: 12px;
}
.detail {
font-size: 12px;
/* font-weight: bold; */
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
padding: 3px 3px 0px 3px;
}
.w-5 {
width: 10%;
}
.w-25 {
width: 50%;
}
</style>
</head>
<body>
<div class="container">
<div class="Company_profile">
<div class="row">
<div class="col-12 heading">
<h1 class="text-white">{{request.user.company.print_name}}</h1>
</div>
</div>
<div class="row">
<table style="width:100%; border:none;">
<tr style="border:none;">
<td style="width:33.33%; border:none;"></td>
<td style="width:33.33%; border:none; text-align:center;">Email : {{request.user.company.email}}
</td>
<td style="width:33.33%; border:none; text-align:right;">Times Printed :1</td>
</tr>
<tr style="border:none;">
<td style="width:33.33%; border:none;"></td>
<td style="width:33.33%; border:none; text-align:center;">VAT Registration No: {{request.user.company.it_pan}}</td>
<td style="width:33.33%; border:none; text-align:right;">Copy of Original</td>
</tr>
</table>
</div>
<div class="row">
<div class="col-12 subheading">
<h3 class="float-left">कर बिजक</h3>
</div>
</div>
</div>
<div class="Customer_info">
<div class="row">
<table style="width:100%; border:none;">
<tr style="border:none;">
<td style="width:50%; border:none;" class="detail">Invoice No.:{{purchase_data.voucherno}}</td>
<td style="width:50%; border:none; text-align:right;" class="detail">Invoice Date :{{purchase_data.formatted_nepalidate}}</td>
</tr>
<tr style="border:none;">
<td style="width:50%; border:none;" class="detail">Customer Name:{{purchase_data.party.name}}</td>
<td style="width:50%; border:none;"></td>
</tr>
<tr style="border:none;">
<td style="width:50%; border:none;" class="detail">Customer Address :{{purchase_data.party.address}}</td>
<td style="width:50%; border:none;"></td>
</tr>
<tr style="border:none;">
<td style="width:50%; border:none;" class="detail">Customer's VAT/PAN No:{{purchase_data.party.it_pan}}</td>
<td style="width:50%; border:none;"></td>
</tr>
</table>
</div>
</div>
<br />
<br />
<div class="Sales_detail">
<table style="width:100%;">
<thead>
<tr>
<th class="w-5">S.N</th>
<th class="w-25">Particulars</th>
<th class="w-5">Qty</th>
<th class="w-5">Unit </th>
<th class="w-5"> Price</th>
<th class="w-5">Amount</th>
</tr>
</thead>
<tbody>
{% for purchaseItem in purchase_items %}
<tr>
<td scope="row">{{ forloop.counter }}</th>
<td>{{purchaseItem.item.name}}</td>
<td>{{purchaseItem.quantity}}</td>
<td>{{purchaseItem.unit.name}}</td>
<td>{{purchaseItem.formatted_price}}</td>
<td>{% if purchase_data.is_line_discount %}{{purchaseItem.formatted_amount}}{% else %}{{purchaseItem.formatted_amount}}{% endif %}</td>
</tr>
{% endfor %}
<tr>
<td colspan="5" style="text-align:left;">Total Amount</td>
<td>{{purchase_data.total_amount}}</td>
</tr>
<tr>
<td colspan="5" style="text-align:left;">Discount Amount</td>
<td>{{purchase_data.trade_discount_amount}}</td>
</tr>
<tr>
<td colspan="5" style="text-align:left;">Total Taxable Amount</td>
<td>{{purchase_data.taxable_amount}}</td>
</tr>
<tr>
<td colspan="5" style="text-align:left;">13% VAT</td>
<td>{{purchase_data.vat_amount}}</td>
</tr>
<tr>
<td colspan="5" style="text-align:left; font-weight:Bold;">Total NPR Incl. VAT</td>
<td style="font-weight:Bold;">{{purchase_data.total_amount_inc_vat}}</td>
</tr>
</tbody>
</table>
<div class="row">
<div class="col-12 detail">
<p><b>Total Amount in Words :</b> {{purchase_data.formatted_amountwords}}
</p>
</div>
</div>
</div>
<div class="footer" style="margin-top:100px ;">
<table style="width:100%; border:none;">
<tr style="border:none;">
<td style="width:100%; border:none;">
<p class="text-left">(E. & O. E) Goods once sold are not exchangeable or returnable.</p>
</td>
</tr>
<tr style="border:none;">
<td style="width:50%; border:none;"></td>
<td style="width:50%; border:none; text-align:right;">-----------------------------</td>
</tr>
<tr style="border:none;">
<td style="width:50%; border:none;"></td>
<td style="width:50%; border:none; text-align:right;">Authorised Signatory</td>
</tr>
<tr style="border:none;">
<td style="width:50%; border:none;"></td>
<td style="width:50%; border:none; text-align:right;">Printed On : {{purchase_data.formatted_nepalidate}}</td>
</tr>
<tr style="border:none;">
<td style="width:50%; border:none;"></td>
<td style="width:50%; border:none; text-align:right;">Printed By: {{request.user.username}}</td>
</tr>
</table>
</div>
</div>
</body>
</html>
I am getting this output with black square but not the text:
I am using imgkit and html/css to make tables containing data from a dataframe. The html/css I have looks as it should when rendered in a browser, but the image rendered by imgkit has horizontal gaps in row 3 and in the green background.
imgkit output::
What it should be like:
The HTML/CSS code
<head>
<style>
#box {
width: 750px;
height: 250px;
display: table;
}
table {
font-family: Arial;
color: #000000;
font-size: 8pt;
padding: 0px;
margin: 0px;
width: 100%;
height: 100%;
min-height: 70%;
border-collapse: collapse;
border-spacing: 0;
}
td, tr {
padding: 0px;
}
.border-right {
border-right: 2px solid #29577e;
}
.border-bottom, .border-bottom td {
border-bottom: 2px solid #29577e;
}
.dash {
border-bottom: 1px dashed #29577e;
}
.bold {
font-weight: bold;
}
.right {
text-align: right;
}
.col-1, .col-2, .col-3, .col-7 {
text-align: left;
}
.col-1 {
}
.indent1 {
text-indent: 20px;
}
.indent2 {
text-indent: 40px;
}
.green {
background-color: #d5eff0;
}
</style>
</head>
<div id="box">
<table cellpadding="0">
<tr class="index top bold">
<th class="col-1"><th>
<th class="col-2"><th>
<th class="col-3"><th>
<th class="col-4 right">L12M<th>
<th class="col-5 right">L3M<th>
<th class="col-6 border-right right">L1M<th>
<th class="col-7"><th>
<th class="col-8 right">L12M<th>
<th class="col-9 right">L3M<th>
<th class="col-10 border-right right">L1M<th>
</tr>
<tr class="index bottom bold">
<th class="col-1">Revenue (K�)<th>
<th class="col-2"><th>
<th class="col-3"><th>
<th class="col-4 right"><th>
<th class="col-5 right"><th>
<th class="col-6 border-right right"><th>
<th class="col-7">BV waterfall (K�)<th>
<th class="col-8 right"><th>
<th class="col-9 right"><th>
<th class=" col-10 border-right right"><th>
</tr>
<tr class="row-1">
<td class="col-1"><td>
<td class="col-2">Total Collections<td>
<td class="col-2"><td>
<td class="col-4 right">147,499<td>
<td class="col-5 right">40,734<td>
<td class="col-6 border-right right">12,116<td>
<td class="col-7 bold indent1 dash">BV (BoP)<td>
<td class="col-8 bold dash right">200,501<td>
<td class="col-9 bold dash right">189,658<td>
<td class="col-10 border-right bold dash right">148,963<td>
</tr>
<tr class="row-2">
<td class="col-1"><td>
<td class="col-2 dash">Amortization<td>
<td class="col-3 dash"><td>
<td class="col-4 dash right">6,499<td>
<td class="col-5 dash right">-1,734<td>
<td class="col-6 border-right dash right">-2,116<td>
<td class="col-7 indent1">New Purchase<td>
<td class="col-8 right">22,342<td>
<td class="col-9 right">3,264<td>
<td class="col-10 border-right right">877<td>
</tr>
<tr class="row-3">
<td class="col-1 border-bottom"><td>
<td class="col-2 border-bottom bold">Revenue<td>
<td class="col-3 border-bottom"><td>
<td class="col-4 border-bottom right">154,499<td>
<td class="col-5 border-bottom right">39,734<td>
<td class="col-6 border-bottom border-right right">9,116<td>
<td class="col-7 green indent2 ">Regular<td>
<td class="col-8 green right">13,704<td>
<td class="col-9 green right">1,701<td>
<td class="col-10 border-right green right">602<td>
</tr>
<tr class="row-4">
<td class="col-1 bold">Costs (K�)<td>
<td class="col-2"><td>
<td class="col-3"><td>
<td class="col-4 right"><td>
<td class="col-5 right"><td>
<td class="col-6 border-right right"><td>
<td class="col-7 green indent2">PRE4<sup>3</sup><td>
<td class="col-8 green right">8,638<td>
<td class="col-9 green right">1,563<td>
<td class="col-10 border-right green right">274<td>
</tr>
<tr class="row-5">
<td class="col-1"><td>
<td class="col-2 bold">Comm<td>
<td class="col-3">FC (c-a)<td>
<td class="col-4 right">32,371<td>
<td class="col-5 right">9,254<td>
<td class="col-6 border-right right">2,875<td>
<td class="col-7 indent1">Purc. Price Changes<td>
<td class="col-8 right">-8,274<td>
<td class="col-9 right">-2,393<td>
<td class="col-10 border-right right">-784<td>
</tr>
<tr class="row-6">
<td class="col-1"><td>
<td class="col-2 dash"><td>
<td class="col-3 dash">Actuals<td>
<td class="col-4 dash right">37,371<td>
<td class="col-5 dash right">11,254<td>
<td class="col-6 border-right dash right">3,875<td>
<td class="col-7 indent1">Amortization<td>
<td class="col-8 right">6,950<td>
<td class="col-9 right">-1,427<td>
<td class="col-10 border-right right">-2,495
<td>
</tr>
<tr class="row-7">
<td class="col-1"><td>
<td class="col-2 bold">Unremitted<td>
<td class="col-3">FC (c-a)<td>
<td class="col-4 right">7,980<td>
<td class="col-5 right">2,300<td>
<td class="col-6 border-right right">541<td>
<td class="col-7 indent2 green">Normal Amortization<td>
<td class="col-8 green right">-32,017<td>
<td class="col-9 green right">-9,167<td>
<td class="col-10 border-right green right">-2,495<td>
</tr>
<tr class="row-8">
<td class="col-1"><td>
<td class="col-2 dash"><td>
<td class="col-3 dash">Actuals<td>
<td class="col-4 dash right">16,700<td>
<td class="col-5 dash right">3,741<td>
<td class="col-6 border-right dash right">756<td>
<td class="col-7 indent2 green">Revaluation<td>
<td class="col-8 green right">38,967<td>
<td class="col-9 green right">7,740<td>
<td class="col-10 border-right green right">0<td>
</tr>
<tr class="row-9">
<td class="col-1"><td>
<td class="col-2 bold">Total<sup>2</sup><td>
<td class="col-3">FC (c-a)<td>
<td class="col-4 right">98,2%<td>
<td class="col-5 right">30,1%<td>
<td class="col-6 border-right right">54,4%<td>
<td class="col-7 dash indent1">Currency Diff. Effect<td>
<td class="col-8 dash right">2,787<td>
<td class="col-9 dash right">-541<td>
<td class="col-10 dash border-right right">4,653<td>
</tr>
<tr class="row-10 border-bottom">
<td class="col-1"><td>
<td class="col-2"><td>
<td class="col-3">Actuals<td>
<td class="col-4 right">16.7%<td>
<td class="col-5 right">41.8%<td>
<td class="col-6 border-right right">75.6%<td>
<td class="col-7 bold indent1">BV (EoP)<td>
<td class="col-8 bold right">194,477<td>
<td class="col-9 bold right">194,477<td>
<td class="col-10 bold border-right right">194,477<td>
</tr>
</table>
</div>
https://codepen.io/maryj25/pen/KKqVKmJ
Any advice how to fix that is appreciated.
I'm trying to do a little script that writes an html file, this file must have a table which contains two rows that have some information about different products, I managed to get this done, but now I need that this table repeats as many times depending on a previous input data, so for this I thought I could multiply the function which contains the html code but it doesn't work, actually I'm not quite sure what I'm doing here so a little bit of help wouldn't be bad...
This is what I want:
Input ---> How many tables: 3
So the html output file should look something like this
<-- TABLE 1 -->
<table>
<tr>
<td colspan="4" height="30"></td>
</tr>
<tr>
<td width="50" class="width6p"></td>
<td width="260" class="width44p"><img src="http://site/image/CODEPRODUCT_1" width="230" alt="DESCRIPTION_1" style="display:block" border="0" class="width90p"/></td>
<td width="30" class="width3p"></td>
<td width="260" class="width44p"><img src="http://site/image/CODEPRODUCT_2" width="230" alt="DESCRIPTION_2" style="display:block" border="0" class="width90p"/></td>
</tr>
<tr>
<td></td>
<td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
<span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_1</span ><br />
<span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_1</span><br/>
<span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-1</span><br />
<span style="font-size:18px;" class="font14">$ </span>
<span style="font-size:24px; line-height:30px;" class="font20">PRICE_1</span>
<span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
</td>
<td></td>
<td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
<span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_2</span><br />
<span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_2</span><br/>
<span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-2</span><br />
<span style="font-size:18px;" class="font14">$ </span>
<span style="font-size:24px; line-height:30px;" class="font20">PRICE_2</span>
<span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
</td>
</tr>
<tr>
<td></td>
<td style="inline-height:0px;padding-top:4px;"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></td>
<td></td>
<td style="inline-height:0px;padding-top:4px;"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></td>
</tr>
</table>
<-- TABLE 2 -->
<table>
<tr>
<td colspan="4" height="30"></td>
</tr>
<tr>
<td width="50" class="width6p"></td>
<td width="260" class="width44p"><img src="http://site/image/CODEPRODUCT_1" width="230" alt="DESCRIPTION_1" style="display:block" border="0" class="width90p"/></td>
<td width="30" class="width3p"></td>
<td width="260" class="width44p"><img src="http://site/image/CODEPRODUCT_2" width="230" alt="DESCRIPTION_2" style="display:block" border="0" class="width90p"/></td>
</tr>
<tr>
<td></td>
<td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
<span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_1</span ><br />
<span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_1</span><br/>
<span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-1</span><br />
<span style="font-size:18px;" class="font14">$ </span>
<span style="font-size:24px; line-height:30px;" class="font20">PRICE_1</span>
<span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
</td>
<td></td>
<td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
<span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_2</span><br />
<span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_2</span><br/>
<span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-2</span><br />
<span style="font-size:18px;" class="font14">$ </span>
<span style="font-size:24px; line-height:30px;" class="font20">PRICE_2</span>
<span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
</td>
</tr>
<tr>
<td></td>
<td style="inline-height:0px;padding-top:4px;"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></td>
<td></td>
<td style="inline-height:0px;padding-top:4px;"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></td>
</tr>
</table>
<-- TABLE 3 -->
<table>
<tr>
<td colspan="4" height="30"></td>
</tr>
<tr>
<td width="50" class="width6p"></td>
<td width="260" class="width44p"><img src="http://site/image/CODEPRODUCT_1" width="230" alt="DESCRIPTION_1" style="display:block" border="0" class="width90p"/></td>
<td width="30" class="width3p"></td>
<td width="260" class="width44p"><img src="http://site/image/CODEPRODUCT_2" width="230" alt="DESCRIPTION_2" style="display:block" border="0" class="width90p"/></td>
</tr>
<tr>
<td></td>
<td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
<span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_1</span ><br />
<span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_1</span><br/>
<span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-1</span><br />
<span style="font-size:18px;" class="font14">$ </span>
<span style="font-size:24px; line-height:30px;" class="font20">PRICE_1</span>
<span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
</td>
<td></td>
<td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
<span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_2</span><br />
<span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_2</span><br/>
<span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-2</span><br />
<span style="font-size:18px;" class="font14">$ </span>
<span style="font-size:24px; line-height:30px;" class="font20">PRICE_2</span>
<span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
</td>
</tr>
<tr>
<td></td>
<td style="inline-height:0px;padding-top:4px;"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></td>
<td></td>
<td style="inline-height:0px;padding-top:4px;"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></td>
</tr>
</table>
Here is my Python code
import locale
import requests
import urlparse
import json
def html(sku_01,desc_01,sku_precio_1,sku_02,desc_02,sku_precio_2,bloque_prod):
f = open('mkt-output.html','w')
f.write(bloque_prod)
f.close()
if __name__ == '__main__':
sku_01 = raw_input('Ingrese SKU: ')
desc_01 = raw_input('Descripcion de SKU: ')
sku_precio_1 = raw_input('Precio de SKU: ')
sku_02 = raw_input('Ingrese SKU: ')
desc_02 = raw_input('Descripcion de SKU: ')
sku_precio_2 = raw_input('Precio de SKU: ')
bloque_prod = """<table>
<tr>
<td colspan="4" height="30"></td>
</tr>
<tr>
<td width="50" class="width6p"></td>
<td width="260" class="width44p"><img src="http://site/images/{}" width="230" alt="{}" style="display:block" border="0" class="width90p"/></td>
<td width="30" class="width3p"></td>
<td width="260" class="width44p"><img src="http://site/images/{}" width="230" alt="{}" style="display:block" border="0" class="width90p"/></td>
</tr>
<tr>
<td></td>
<td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
<span style="font-weight: bold; color:#008EAA" class="font14">{}</span ><br />
<span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14">{} {}</span><br/>
<span style="font-size:12px;" class="font12">SKU: {}-{}</span><br />
<span style="font-size:18px;" class="font14">$ </span>
<span style="font-size:24px; line-height:30px;" class="font20">{}</span>
<span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
</td>
<td></td>
<td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
<span style="font-weight: bold; color:#008EAA" class="font14">{}</span><br />
<span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14">{} {}</span><br/>
<span style="font-size:12px;" class="font12">SKU: {}-{}</span><br />
<span style="font-size:18px;" class="font14">$ </span>
<span style="font-size:24px; line-height:30px;" class="font20">{}</span>
<span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
</td>
</tr>
<tr>
<td></td>
<td style="inline-height:0px;padding-top:4px;"><img src="http://www.site/templates/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></td>
<td></td>
<td style="inline-height:0px;padding-top:4px;"><img src="http://www.site/templates/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></td>
</tr>
</table>""".format(sku_01,
sku_01,
desc_01,
sku_02,
sku_02,
desc_02,
' '.join(desc_01.split()[0:3]),
' '.join(desc_01.split()[3:-1]),
desc_01.split()[-1],
sku_01[0:-1],
sku_01[-1],
sku_precio_1,
' '.join(desc_02.split()[0:3]),
' '.join(desc_02.split()[3:-1]),
desc_02.split()[-1],
sku_02[0:-1],
sku_02[-1],
sku_precio_2,
sku_01,
sku_02)
html(sku_01, desc_01, sku_precio_1, sku_02, desc_02, sku_precio_2, bloque_prod)
If you need duplicate data written to the HTML file, you could simply have the html() function write variable bloque_prod multiple times by multiplying it:
def html(sku_01,desc_01,sku_precio_1,sku_02,desc_02,sku_precio_2,bloque_prod,tables):
f = open('mkt-output.html','w')
f.write(bloque_prod * tables)
f.close()
Note the addition of the variable tables for the number of table duplicates.
Then, define variable tables in __main()__:
tables = input('Tables: ')
…and add tables into the last line where you call html()
html(sku_01, desc_01, sku_precio_1, sku_02, desc_02, sku_precio_2, bloque_prod,tables)
Are you looking to write different tables?
Also, if it's necessary to annotate which table is currently being printed, you could add an if statement in:
def html(sku_01,desc_01,sku_precio_1,sku_02,desc_02,sku_precio_2,bloque_prod,tables):
f = open('mkt-output.html','w')
rawHTML = ""
for table in range(0, tables):
rawHTML += ("\n<-- TABLE " + str(table) + " -->\n" + bloque_prod)
f.write(rawHTML)
f.close()
(if you wanted the numbering to start at 1, you'd just change str(table) to str(table + 1))
--EDIT-- It seems that you are looking to create tables with different values. I would rewrite the program as such to do this:
import locale
import requests
import urlparse
import json
def createTable(sku_01,desc_01,sku_precio_1,sku_02,desc_02,sku_precio_2):
bloque_prod = """<table>
<tr>
<td colspan="4" height="30"></td>
</tr>
<tr>
<td width="50" class="width6p"></td>
<td width="260" class="width44p"><img src="http://site/images/{}" width="230" alt="{}" style="display:block" border="0" class="width90p"/></td>
<td width="30" class="width3p"></td>
<td width="260" class="width44p"><img src="http://site/images/{}" width="230" alt="{}" style="display:block" border="0" class="width90p"/></td>
</tr>
<tr>
<td></td>
<td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
<span style="font-weight: bold; color:#008EAA" class="font14">{}</span ><br />
<span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14">{} {}</span><br/>
<span style="font-size:12px;" class="font12">SKU: {}-{}</span><br />
<span style="font-size:18px;" class="font14">$ </span>
<span style="font-size:24px; line-height:30px;" class="font20">{}</span>
<span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
</td>
<td></td>
<td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
<span style="font-weight: bold; color:#008EAA" class="font14">{}</span><br />
<span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14">{} {}</span><br/>
<span style="font-size:12px;" class="font12">SKU: {}-{}</span><br />
<span style="font-size:18px;" class="font14">$ </span>
<span style="font-size:24px; line-height:30px;" class="font20">{}</span>
<span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
</td>
</tr>
<tr>
<td></td>
<td style="inline-height:0px;padding-top:4px;"><img src="http://www.site/templates/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></td>
<td></td>
<td style="inline-height:0px;padding-top:4px;"><img src="http://www.site/templates/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></td>
</tr>
</table>""".format(sku_01,
sku_01,
desc_01,
sku_02,
sku_02,
desc_02,
' '.join(desc_01.split()[0:3]),
' '.join(desc_01.split()[3:-1]),
desc_01.split()[-1],
sku_01[0:-1],
sku_01[-1],
sku_precio_1,
' '.join(desc_02.split()[0:3]),
' '.join(desc_02.split()[3:-1]),
desc_02.split()[-1],
sku_02[0:-1],
sku_02[-1],
sku_precio_2,
sku_01,
sku_02)
return bloque_prod
if __name__ == "__main__":
f = open('mkt-output.html','w+') # Open file in w+ mode so we can append to the end
for table in range(0,input("Tables: ")):
print ("--Table "+str(table)+"--")
sku_01 = raw_input('Ingrese SKU: ')
desc_01 = raw_input('Descripcion de SKU: ')
sku_precio_1 = raw_input('Precio de SKU: ')
sku_02 = raw_input('Ingrese SKU: ')
desc_02 = raw_input('Descripcion de SKU: ')
sku_precio_2 = raw_input('Precio de SKU: ')
f.write(createTable(sku_01,desc_01,sku_precio_1,sku_02,desc_02,sku_precio_2))
f.close()
Hope that helps.
Code I'm using for PDF generation:
html = HTML(string=final_html, base_url=request.build_absolute_uri())
main_doc = html.render()
pdf = main_doc.write_pdf()
This is the content of final_html string:
<body style="width:100%; height:100%;">
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
font-family: 'Open Sans', sans-serif;
font-size: 14px;
}
table {
margin-top: 0px;
}
th, td {
padding: 5px;
}
.bottom {
vertical-align: bottom;
}
tr.noBorder td {
border: 0;
}
</style> <table style="width:100%; height:100%;">
<tr>
<td COLSPAN="2" style="border-right-style: hidden;">
<div style="float: left; display:inline;">
<div>
<div><strong>-seller_name-</strong></div>
</div>
</div>
</td>
<td COLSPAN=2>
<div style="float: right; display:inline;">
<div style="text-align: center">
<strong>-label_name-</strong>
</div>
<div>
-crnbarcodeimage-
</div>
<div style="text-align: center">
<strong>*-label_number-*</strong>
</div>
</div>
</td>
</tr>
<tr>
<td COLSPAN=2>Name & Delivery Address</td>
<td style="border-right-style: hidden;">Payment Mode</td>
<td style="float: right; border-left-style: hidden; border-top-style: hidden; border-bottom-style: hidden;">
<strong>-order_type-</strong></td>
</tr>
<tr>
<td COLSPAN=2>
<div><strong>-drop_name-</strong></div>
<br>
<div>-drop_address-</div>
<br>
<div>-drop_state- <strong>-drop_pincode-</strong></div>
<br>
<div><strong>Contact Number: -drop_phone-</strong></div>
</td>
<td valign="top" COLSPAN=2>
<div style="float: left;">
<strong>Order No.:</strong>
</div>
<div style="float: right;">
-seller_order_id-
</div>
<div>
<div>
-seller_order_id_barcode-
</div>
</div>
<div style="float: left;">
<strong>Invoice No.</strong>
</div>
<div style="float: right;">
-invoice_number-
</div>
</td>
</tr>
<tr>
<td COLSPAN=4 ALIGN=RIGHT>
</td>
</tr>
<tr>
<td>Description</td>
<td>QTY</td>
<td>Rate</td>
<td>Amount</td>
</tr>
<tr>
<td>-item-</td>
<td>1</td>
<td>-invoice_value-</td>
<td>-invoice_value-</td>
</tr>
<tr>
<td COLSPAN=3 ALIGN=LEFT style="border-right-style:hidden;">Total</td>
<td COLSPAN=1 ALIGN=LEFT style="border-left-style:hidden;">-invoice_value-</td>
</tr>
<tr>
<td COLSPAN=3 ALIGN=LEFT style="border-right-style:hidden;"><strong>COD Amount</strong></td>
<td COLSPAN=1 ALIGN=LEFT style="border-left-style:hidden;"><strong>-cod_value-</strong></td>
</tr>
<tr>
<td COLSPAN=4>
Prices are inclusive of all applicable taxes
</td>
</tr>
<tr>
<td COLSPAN=4 style="border-bottom-style:hidden;">If Undelivered, please return to:</td>
</tr>
<tr>
<td COLSPAN=4>
<strong>
<div>B-220/2, 1st Floor, Right Door, Savitri Nagar, New Delhi: 110017 Ph. 8376035546</div>
</strong>
</td>
</tr> </table> </body> </html>
The pdf is always generated as partial of the page while I want it to cover entire pdf page.
I think that you might have to add this to your styles to configure the page:
#page {
size: 11cm 14.1cm;
margin-left: 0.5cm;
margin-top: 0.5cm;
}
I Need reslove 3 problem:
I'm trying to write a simple program in python that parses a web page for the table of table id="dgContract".
store this table of page 1 ,page2,page3....pageN to mongodb, i don't know how to opration mongodb.
parses Selected "Detail" links of content store to mongodb.if open Selected "view" ,need add http://www.xxx.com/ like http://www.xxx.com/LicConDisp.aspx?CID=xxxxx
open picture in new windows will clear!!!
my code:
import urllib2,cookielib,sys
import urllib,string
import cStringIO,Image,re
import BeautifulSoup # For processing HTML
from BeautifulSoup import BeautifulStoneSoup # For processing XML
from BeautifulSoup import BeautifulSoup
import configparser
from pymongo import Connection
import codecs
import sitecustomize
import chardet
host = 'localhost'
database = 'test'
collection = 'compinfo'
f=file('copy of out4.html','r')
html=f.read()
soup = BeautifulSoup(''.join(html))
table = soup.find('table', id="dgContract")
rows = table.findAll('tr')
store = []
for tr in rows:
cols = tr.findAll('td')
row = []
for td in cols:
try:
row.append(''.join(td.find(text=True)))
except Exception:
row.append('')
store.append('|'.join(row))
print '\n'.join(store)
But output like this: open picture in new windows will clear!!!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HEAD>
<title>查询</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<link href="css/user.css" type="text/css" rel="stylesheet">
<style type="text/css">
.STYLE1 {FONT-SIZE: 12px; COLOR: #ffffff}
.STYLE2 {FONT-SIZE: 14px; COLOR: #000000}
.STYLE45 {FONT-SIZE: 12px}
.STYLE51 {FONT-WEIGHT: bold; FONT-SIZE: 12px; FONT-FAMILY: "宋体"}
.STYLE52 {FONT-WEIGHT: bold; FONT-SIZE: 12px; COLOR: #ffffff; FONT-FAMILY: "宋体"}
</style>
</HEAD>
<body background="images/bg.jpg" MS_POSITIONING="GridLayout">
<form name="Form1" method="post" action="ContractSearcher.aspx" id="Form1">
<div align="center">
<table borderColor="#c7c7c7" cellSpacing="0" cellPadding="0" border="1">
<tr>
<td class="tdBorder">
<!-- content -->
<!--显示用户信息条 -->
<!--内容主体:左侧为菜单,右侧为内容显示区 -->
<table height="350" cellSpacing="0" cellPadding="0" width="760" border="0">
<tr>
<!--左侧菜单项 -->
<td width="3"> </td>
<!--右侧内容显示区 -->
<td vAlign="top" width="815" bgColor="#ffffff">
<table width="100%">
<tr>
<td class="tdbigmidcenter">
<table class="tablebigContent" cellspacing="0" rules="all" border="1" id="dgContract" width="815">
<tr bgcolor="PapayaWhip">
<td>numb</td><td>用户1</td><td>用户2</td>
<td>作者</td>
<td align="center">接受时间</td><td align="center">发送</td>
<td align="center">详情</td>
<td align="center">状态</td>
<td>version</td>
</tr>
<tr>
<td width="21%">HOPE-HT-YX-S-140331-120</td><td width="14%">
A公司
</td><td width="14%">A学校</td><td width="5%">david</td><td align="center" width="10%">
2014-3-31
</td><td align="center" width="10%">
未发送
</td><td align="center" width="7%">
查看
</td><td align="center" width="10%">
<a title="" href="ConStatusDisp.aspx?CID=91e13d7a-e812-428d-a5c2-532778ea4e89">已结束[<font color=red>通过</font>]</a>
</td><td align="center" width="5%">
1.0
</td>
</tr><tr>
<td width="21%">HOPE-HT-YX-S-140328-106</td>
<td width="14%">
A公司
</td>
<td width="14%">M公司</td><td width="5%">王明</td><td align="center" width="10%">
2014-3-28
</td><td align="center" width="10%">
未发货
</td><td align="center" width="7%">
查看
</td><td align="center" width="10%">
<a title="" href="ConStatusDisp.aspx?CID=72648278-dbe3-4577-9154-23182e349b33">已结束[<font color=red>HOPECE140328-5 </font>]</a>
</td><td align="center" width="5%">
1.0
</td>
</tr><tr>
<td width="21%"> </td>
<td width="14%">
B公司
</td>
<td width="14%">C中心</td><td width="5%">王明</td><td align="center" width="10%">
2014-3-12
</td><td align="center" width="10%">
2014-3-28<br>
[<font color=deeppink><strong>全<strong></font>]
</td><td align="center" width="7%">
查看
</td><td align="center" width="10%">
<a title="9900014479" href="ConStatusDisp.aspx?CID=0526a587-85dc-484e-88f4-87967546678f">已结束[<font color=red>HOPETE140313-1 </font>][<font color=deeppink><strong>全<strong></font>]</a>
</td><td align="center" width="5%">
1.0
</td>
</tr><tr>
<td width="21%">HOPE-HT-YX-S-140306-001</td>
<td width="14%">
A公司
</td>
<td width="14%">A中心</td>
<td width="5%">JACK</td><td align="center" width="10%">
2014-3-7
</td><td align="center" width="10%">
2014-3-28<br>
[<font color=deeppink><strong>全<strong></font>]
</td><td align="center" width="7%">
查看
</td><td align="center" width="10%">
<a title="9900014479" href="ConStatusDisp.aspx?CID=dfec1630-e1d4-478c-9feb-415eedbd6184">已结束[<font color=red>HOPETE140317-4 </font>][<font color=deeppink><strong>全<strong></font>]</a>
</td><td align="center" width="5%">
1.0
</td>
</tr><tr>
<td width="21%">HOPE-HT-YX-S-140228-102</td>
<td width="14%">
G公司
</td>
<td width="14%">F公司</td>
<td width="5%">david</td><td align="center" width="10%">
未通过
</td><td align="center" width="10%">
未发货
</td><td align="center" width="7%">
查看
</td><td align="center" width="10%">
<a title="" href="ConStatusDisp.aspx?CID=9e19e1c9-7644-4392-9bdd-89e2bac346cd">已作废</a>
</td><td align="center" width="5%">
1.0
</td>
</tr><tr>
<td width="21%">HOPE-HT-YX-S-140228-005</td>
<td width="14%">
T公司
</td>
<td width="14%">J公司 </td>
<td width="5%">jack</td><td align="center" width="10%">
2014-2-28
</td><td align="center" width="10%">
2014-3-28<br>
[<font color=deeppink><strong>全<strong></font>]
</td><td align="center" width="7%">
查看
</td><td align="center" width="10%">
<a title="9900014480" href="ConStatusDisp.aspx?CID=45039bfb-ccb8-49f4-b8fe-27bc8cf59803">已结束[<font color=red>HOPECE140228-10</font>][<font color=deeppink><strong>全<strong></font>]</a>
</td><td align="center" width="5%">
1.0
</td>
</tr><tr>
<td width="21%">HOPE-HT-YX-S-140228-002</td>
<td width="14%">
S公司
</td>
<td width="14%">V公司</td>
<td width="5%">张军</td><td align="center" width="10%">
2014-2-28
</td><td align="center" width="10%">
2014-3-28<br>
[<font color=deeppink><strong>全<strong></font>]
</td><td align="center" width="7%">
查看
</td><td align="center" width="10%">
<a title="9900014479" href="ConStatusDisp.aspx?CID=02a8d406-a826-4a5a-b466-f4bca2640307">已结束[<font color=red>HOPETE140307-4 </font>][<font color=deeppink><strong>全<strong></font>]</a>
</td><td align="center" width="5%">
1.0
</td>
</tr><tr>
<td width="21%"> </td>
<td width="14%">
A公司
</td>
<td width="14%">W公司</td><td width="5%">jack</td><td align="center" width="10%">
2014-2-28
</td><td align="center" width="10%">
2014-3-28<br>
[<font color=deeppink><strong>全<strong></font>]
</td><td align="center" width="7%">
查看
</td><td align="center" width="10%">
<a title="9900014479" href="ConStatusDisp.aspx?CID=2684c70a-baea-4da4-911b-19cdbe627fd9">已结束[<font color=red>HOPETE140307-3 </font>][<font color=deeppink><strong>全<strong></font>]</a>
</td><td align="center" width="5%">
1.0
</td>
</tr><tr>
<td width="21%">HOPE-HT-YX-S-140228-013</td>
<td width="14%">
B公司
</td><td width="14%">V公司</td>
<td width="5%">rose</td><td align="center" width="10%">
2014-2-28
</td><td align="center" width="10%">
2014-3-28<br>
[<font color=deeppink><strong>全<strong></font>]
</td><td align="center" width="7%">
查看
</td><td align="center" width="10%">
<a title="9900014479" href="ConStatusDisp.aspx?CID=1204ad5e-4552-43af-a650-19b93f9d2514">已结束[<font color=red>HOPETE140307-2 </font>][<font color=deeppink><strong>全<strong></font>]</a>
</td><td align="center" width="5%">
1.0
</td>
</tr><tr>
<td width="21%">HOPE-HT-YX-S-140226-018</td>
<td width="14%">
C公司
</td><td width="14%">A中心</td>
<td width="5%">david</td><td align="center" width="10%">
2014-2-26
</td><td align="center" width="10%">
2014-3-14<br>
[<font color=deeppink><strong>全<strong></font>]
</td><td align="center" width="7%">
查看
</td><td align="center" width="10%">
<a title="9900014388" href="ConStatusDisp.aspx?CID=a04cdcd2-5b5c-4182-a22d-a29399ab6991">已结束[<font color=red>HOPECE140228-1 </font>][<font color=deeppink><strong>全<strong></font>]</a>
</td><td align="center" width="5%">
1.0
</td>
</tr><tr align="right">
<td colspan="9"><span>1</span> 2 3</td>
</tr>