- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在用 Django 制作一个应用程序。我想在浏览选择链接时生成 pdf。所以我使用 weasyprint 模块来转换我的 pdf。我导入 weasyprint 模块。但是当我运行那个应用程序时,我发现了一些错误。这是views.py文件
def customersProfile(request, customer_id, sys_type):
sys_type=sys_type
area="profile"
site_credit = site_credit1
time_now=timezone.now()
customers=get_object_or_404(CustomerInfo, pk=customer_id)
html_string = render_to_string('shop/profile.html', {'customers': customers, 'todaydate': today_date,'site_name': 'Moon Telecom', 'time_now': time_now, 'site_credit': site_credit, 'area': area})
html = HTML(string=html_string)
result = html.write_pdf()
response = HttpResponse(content_type='application/pdf;')
response['Content-Disposition'] = 'inline; filename=list_people.pdf'
response['Content-Transfer-Encoding'] = 'binary'
with tempfile.NamedTemporaryFile(delete=True) as output:
output.write(result)
output.flush()
output = open(output.name, 'r')
response.write(output.read())
return response
这是HTML 文件。我想创建 pdf 这个 html 文件。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body style>
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="site_title" style="text-align:center; margin:0 auto">
<h3>মুন টেলিকম</h3>
<h3>
<a href="{% url 'shop:index' %}" target="_blank">{{site_name}}</a>
</h3>
<div class="invoice_info_one" style="width:100%; text-align:center">
<p>সকল প্রকার মোবাইল সেট, সীম কার্ড, মেমোরী কার্ড, MP-3, সোলার চার্জার, সোলার ফ্যান, মোবাইল ফোনের ব্যাটারী, চার্জার, ক্যাচিং,কাভার,হেডফোন, রেবন, ডিসপ্লে এবং ইলেট্রিক মালামাল বিক্রেতা</p>
</div>
<div class="invoice_location">
<p>জাহাঙ্গীর সুপার মার্কেট, ব্রীজ রোড, ফুলহাতা বাজার, মোডেলগঞ্জ।</p>
</div>
<div class="invoice_contact">
<p>01717-051200(সুকান্ত) 01828-163858(দোকান)</p>
<p><b>Email:</b> moontelecom2008@gmail.com</p>
</div>
<div class="copy_right">
{% if site_credit %}
<p>{{site_credit}}</p>
{% else %}
<a href="{% url 'shop:nothing' %}">#</a>
{% endif %}
<p></p>
</div>
<hr>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="customer_info">
{% if customers.customer_name %}
<h2 style="font-size:15px">Customer Name:
<b> {{customers.customer_name}}</b>
</h2>
{% else %} <p> Name: দেওয়া হয়নি</p> {% endif %}
<p style="font-size:14px">Phone Number:
{% if customers.customer_mobile_no %}
{{customers.customer_mobile_no}}
{% else %}
No Mobile Number
{% endif %}
</p>
<p style="font-size:14px">Purchase Time: {{customers.customer_sell_date}}</p>
<p style="font-size:14px">invoice id: <b>{{customers.customer_uid}}</b></p>
<p>{{customers.product_warrenty}}</p>
</div>
</div>
</div>
</div>
<div class="table table-bordered">
<table class="table">
<thead>
<tr>
<th>Product Name</th>
<th>Product ID</th>
<th>Warrenty</th>
<th>QN</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{customers.customer_product_name}}</td>
<td>{{customers.customer_product_id}}</td>
<td>{% if customers.customer_product_warrenty == '' %}
<b>No Warrenty</b>
{% else %}
<b> {{customers.customer_product_warrenty}}</b> Month</td>
{% endif %}
<td>{{customers.customer_product_quantity}}</td>
<td>{{customers.customer_price}} Taka</td>
</tr>
</tbody>
<tr>
<td colspan="3"></td>
<td colspan="1">
<b>Total Amount</b>
</td>
<td colspan="1">
<b>{{customers.customer_price}}</b> Taka</td>
</tr>
</table>
{% if customers.customer_conditions %}
<div class="warning">
<p style="font-size:8px; width:500px;line-height:15px;"><i>{{customers.customer_product_name}} {{customers.customer_conditions}}</i></p>
</div>
{% else %} {% endif %}
</div>
</div>
<div class="footer">
<div class="footer_info">
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="footer_info">
<p>Print date: {{time_now}}</p>
<p>
<b>বিঃদ্রঃ ডেলিভারির সময় মাল বুঝিয়া নিবেন। পরে কোন আপত্তী গ্রহনযোগ্য নয়</b>
</p>
<h5>বিক্রিত মাল ফেরত নেওয়া হয় না</h5>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
当我浏览那个 url 时。我收到了这个错误。我试图解决这个错误。但我不能。
UnicodeDecodeError at /shop/1/customerpage/customerprofile/
'utf-8' codec can't decode byte 0xe2 in position 10: invalid continuation byte
Request Method: GET
Request URL: http://127.0.0.1:8000/shop/1/customerpage/customerprofile/
Django Version: 1.11.6
Exception Type: UnicodeDecodeError
Exception Value:
'utf-8' codec can't decode byte 0xe2 in position 10: invalid continuation byte
这是完整的tracebook环境:
Request Method: GET
Request URL: http://127.0.0.1:8000/shop/1/customerpage/customerprofile/
Django Version: 1.11.6
Python Version: 3.5.2
Installed Applications:
['shop.apps.ShopConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'crispy_forms',
'wkhtmltopdf']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/asad/Desktop/smartshop/django-app/shop/views.py" in customersProfile
333. response.write(output.read())
File "/usr/lib/python3.5/codecs.py" in decode
321. (result, consumed) = self._buffer_decode(data, self.errors, final)
Exception Type: UnicodeDecodeError at /shop/1/customerpage/customerprofile/
Exception Value: 'utf-8' codec can't decode byte 0xe2 in position 10: invalid continuation byte
最佳答案
您正在以文本模式阅读 pdf 文件 ('r'
)
>>> response = HttpResponse(content_type='application/pdf')
>>> output = open('example.pdf', 'r')
>>> response.write(output.read())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
...
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2 in position 10: invalid continuation byte
但你需要使用二进制模式('rb'
)
>>> response = HttpResponse(content_type='application/pdf')
>>> output = open('example.pdf', 'rb')
>>> response.write(output.read())
当调用HttpResponse.write
时,检查它的输入是str
还是bytes
;如果输入是 str
它将尝试对其进行编码,在这种情况下会导致错误。通过以二进制模式打开文件,您可以确保输入的是 bytes
并且不会尝试编码。
关于python - Unicode解码错误: Django Generate PDF with weasyprint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46746223/
我的代码有一些问题。我正在尝试遍历包含许多 PDF 的 Drive 文件夹,然后将它们合并为一个文件。当我使用我的代码时,它只是为 Drive 文件夹中的最后一个 PDF 创建一个 PDF,而不是按预
我从 PDF Specification 获取了 PDF 规范中的最小 PDF 示例。 ,将其复制到记事本,将文件重命名为扩展名为 .pdf。 我可以用其他 PDF 查看器(PDF-XChange、S
感谢您在以下方面的帮助: 我有 2 个部分可访问的 PDF(包含标签),我想使用一些命令行工具(如 PDFtk 或 Ghostscript,或任何 Perl 模块)将它们连接起来: 我已经尝试使用 P
我想使用 ghostscript 将矢量 pdf 转换为光栅 pdf(即光栅化矢量 pdf)。但是即使我添加了解析参数 -r300,我也找不到合适的参数来执行此操作。 我使用的代码是-dSAFER -
我无法在 FAQ 中找到这个功能是否存在于 API 中,尽管它在书中提到作为潜在可用的东西。有没有人有任何实现此功能的经验? 最佳答案 在 This thread (日期为 2007 年 6 月)Pa
我要放文件sample.pdf在我的网站上,并希望使用 pdf.js 显示它.我想要的是显示我自己的文件,如 demo ,带有工具栏,放大/缩小等。到目前为止,我还不能这样做。 我确实检查了 hell
我知道这可能不是严格意义上的编程问题(也许是,我不知道)但我在尝试转换常规 pdf(带有超链接、书签、图像、嵌入字体等)时遇到了严重问题.) 转换为 PDF/A-1 格式。 当我用 pdfaPilot
这是 PDF.js 网站 https://github.com/mozilla/pdf.js 我正在搜索和阅读很多文章,大多数编码都是将 pdf 导入 pdf.js 并在浏览器上显示,我不明白是不是
谁能建议我如何将扫描图像转换为可搜索图像或如何将扫描 pdf 转换为可搜索 pdf? 很长一段时间以来,我一直陷入这种情况。 我已经在 ubuntu 中尝试过 pdfocr 应用程序,但没有成功。 最
作为我对客户端/服务器 pdf 签名研究的一部分,我测试了 itext pdf 延迟签名示例。不幸的是,我生成的 pdf 即合并空签名 pdf 和哈希值的输出显示无效签名。 我的代码片段如下 cla
我想将一个 PDF 页面插入到另一个已缩放的 PDF 页面中。我想使用 iTextSharp 来实现此目的。 我有一个矢量绘图,可以导出为单页 PDF 文件。我想将此文件添加到其他 PDF 文档的页面
作为我对客户端/服务器 pdf 签名研究的一部分,我测试了 itext pdf 延迟签名示例。不幸的是,我生成的 pdf 即合并空签名 pdf 和哈希值的输出显示无效签名。 我的代码片段如下 cla
我想为 Kindle 转换电子书。我尝试使用 Calibre 将具有复杂格式样式和图像的基于两种语言的基于文本的大型 PDF 电子书转换为适用于 Kindle 的 AZW3 电子书,并且还尝试了亚马逊
我在 Google Chrome 中显示 pdf 时遇到问题。问题是 Chrome 将 pdf 的某些页面显示为黑色。 启用 Chrome PDF 查看器时会发生这种情况。如果我禁用此插件并使用 Ad
我确信这个问题无处不在,尽管我似乎找不到答案。我希望我的 PDF 文档在 PDF 阅读器中显示时没有空白页,但随后在封面后打印空白页,这样打印出来的文档在右侧甚至左侧都有奇数页。还有其他人遇到过这个问
我需要自动裁剪 pdf 文件(去除白边)。到目前为止,我尝试了两种并不完美的工具: pdf裁剪 问题:它不会裁剪某些 pdf。 pdf-crop-margins 问题:有时它裁剪得太多(精细的细节)。
This PDF由几个源文件组成。其中五个是包含 alpha channel 的 PNG。一种是没有 alpha channel 的 PNG。最后一 block 是带有透明效果的 Photoshop
我的团队将内部 wiki 页面用于各种内容。这些页面是使用 MediaWiki 创建的。我想知道是否有任何方法可以将 wiki 页面转换为 PDF 格式。我必须用它来将用户文档转换为 PDF 格式,以
我希望能够从我可能在数据库或 xml 或任何其他结构化形式中拥有的数据生成高度图形化(也包含大量文本内容)的 PDF 文件。 目前,我们的平面设计师在将内容作为 MS Word 文档后,在 Photo
我正在寻找可以帮助我找到重复 PDF 的实用程序。问题:我有 1000 个 PDF 文件。有些是重复的。由于不同的文件名和文件大小的微小差异,它们不容易被检测到。是否有实用程序/算法/库可以帮助我找到
我是一名优秀的程序员,十分优秀!