1.django自带方法:http://docs.djangoproject.com/en/dev/howto/outputting-pdf/
需要安装包ReportLab,只需简单的几行,但没发现中文解决方法
2.包PDFlib http://www.pdflib.org/
3.HTMLdoc http://www.htmldoc.org/ 这是个命令行脚本。
4.pisa http://pypi.python.org/pypi/pisa/3.0.10,可解决中文pdf转发问题
sudo easy_install pisa
需要的安装包:
ReportLab http://pypi.python.org/pypi/reportlab
Html5lib http://code.google.com/p/html5lib/
pisa http://pypi.python.org/pypi/pisa/3.0.10
def write_pdf(template_src, ctx):
#########Set FONT (微软雅黑)#################
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
#path_to_your_font = '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc'
path_to_your_font = 'baojia/wqy-zenhei.ttc'
pdfmetrics.registerFont(TTFont('msyh', path_to_your_font))
from sx.pisa3 import pisa_default
pisa_default.DEFAULT_FONT["helvetica"]="msyh"
import cStringIO
import ho.pisa as pisa
template = get_template(template_src)
html = template.render(ctx)
result = cStringIO.StringIO()
#如果需要访问远程资源需要将远程资源读到本地,作为临时文件保存
#读取方法:pisa.pisaLinkLoader(url).getFileName
url = ''
pdf = pisa.pisaDocument(cStringIO.StringIO(
html.encode("UTF-8")), result,link_callback = pisa.pisaLinkLoader(url).getFileName)
#生成PDF
if not pdf.err:
response = HttpResponse(result.getvalue(), mimetype='application/pdf')
now = datetime.datetime.now()
response['Content-Disposition'] = 'attachment; filename=baojia%s.pdf' % now.strftime('%Y%m%d%H%M%S')
return response
else:
return HttpResponse('error')
2-8行是为了解决中文字体问题
Freebsd 安装文泉驿字体
%cd /usr/ports/x11-fonts/wqy
make install clean
中文解决参考文档:
http://edisonlz.javaeye.com/blog/635745
http://wiki.woodpecker.org.cn/moin/MiscItems/2008-08-08
另附 很简单的html生成pdf的unix命令wkhtmltopdf
官方网址: https://code.google.com/p/wkhtmltopdf/
http://www.lovelucy.info/wkhtmltopdf-and-pdf2htmlex.html
在Ubuntu及ArchLinux上直接下指令安裝即可:
apt-get install wkhtmltopdf
使用方法:
wkhtmltopdf --help
使用方法很简单:在shell中直接wkhtmltopdf 源文件 目标文件 就可以了
分享到:
相关推荐
Lesson 6 - Capstone project: your first Python program-convert hours to minutes UNIT 2 - STRINGS, TUPLES, AND INTERACTING WITH THE USER Lesson 7 - Introducing string objects: sequences of characters ...
detail how to use the Inkscape drawing tool to convert shapes traced from a photographic image into a sequence of points which reproduce the shape in Python. Once a line is expressed as a Python ...
Apply geometric transformations to images, perform image filtering, and convert an image into a cartoon-like image Detect and track various body parts such as the face, nose, eyes, ears, and mouth ...
interactively (in Gephi), and it concludes by presenting a network of Wikipedia pages related to complex networks. In Part II, you’ll look into networks based on explicit relationships (such as ...
edit, and optimize a vector layer for faster queries, reproject a vector layer, reduce the number of vertices in a vector layer without losing critical data, and convert a raster to a vector....
I’m not actually a firm believer in being to able to specify the “right” tool for the job. Each programming job is different and may well have a number of different solutions and possible tools ...
If you’re a scientist who programs with Python, this practical guide not only teaches you the fundamental parts of SciPy and libraries related to it, but also gives you a taste for beautiful, easy-to...
You will also see how to use pytest in existing unittest-based test suites and will learn some tricks to make the jump to a pytest-style test suite quickly and easily. What you will learn Write ...
This book will demonstrate how to develop a series of web application projects with the Python web micro-framework, and leverage extensions and external Python libraries and APIs to extend the ...
If you’re a scientist who programs with Python, this practical guide, Elegant SciPy not only teaches you the fundamental parts of SciPy and libraries related to it, but also gives you a taste for ...
How to Use This Book ................................................................................................................. 4 The Featured Programs ............................................
How can we use a class Foo in a header file without access to its definition? We can declare data members of type Foo* or Foo&. We can declare (but not define) functions with arguments, and/or ...
caj2pdf Why 的某些文献(多为学位论文)仅提供其专有的 CAJ 格式下载,仅能使用知网提供的软件(如 等)打开,给文献的阅读和管理带来了不便(尤其是在非 Windows 系统上)。 若要将 CAJ 文件转换为 PDF 文件,可以...
GitBook 是一个基于 Node.js 的命令行工具,可使用 Github/Git 和 Markdown 来制作精美的电子书,GitBook 并非关于 Git 的教程。 使用GitBook生成的电子书... How to Make an Operating System 标签:GitBook