问题描述
我试图将我创建的html文档转换为使用报告实验室的pdf。下面是html文档。我不确定如何做到这一点,我看了网上,似乎找不到解决方案。
html文档
<!doctype html>
< html lang =en>
< head>
< meta charset =utf-8>
< title>转换为Pdf< / title>
< / head>
< body>
< h2>转换为pdf< / h2>
< p> Lorem ipsum dolor坐在amet,consectetur adipiscing elit。在tempus massa完成。 Quisque congue dui venenatis rutrum imperdiet。 Nulla争论大会坐在amet magna posuere,在元素felis dapibus。 Mauris maximus feugiat lorem,bibendum orci fringilla a。 Pellentesque rhoncus dignissim tempus。 Aliquam Semper convallis odio ut pharetra。 Nunc在bounndum ornare的bibendum neque。 Curabitur lobortis odio ac turpis tincidunt,在venenatis nibh blandit。整型id arcu maximus,布尔迪特urna ut,tempor odio。 Pellentesque tempus,mi finibus pellentesque,ex magna lacinia elit,semper nibh orci non nulla。 Nunc felis metus,争论一个个人简历,porttitor pellentesque sem。福克斯车辆tincidunt dolor at dictum。 Integer cursus,risus quis finibus dapibus,nulla dolor dapibus massa,et luctus enim dui a nunc。 sed facilisis sapien at risus commodo,eget sollicitudin ex eleifend。 Proin ipsum ipsum,mauris vel中的调味品,rutrum aliquam magna。
Aenean ac odio ante。 Proin eget urna est。Fusce at dui dignissim,tincidunt magna eget,dictum nisl。 Donec enim ipsum,feugiat a tristique vitae,suscipit non risus。 Pellentesque libero leo,pellentesque ut neque ut,pharetra volutpat ex。 Pellentesque purus neque,varius eu dolor eu,placerat ullamcorper velit。 Etiam volutpat blandit tortor non pellentesque。 Donec ac risus lacus。 Pellentesque sagittis vitae odio quis vulputate。 Praesent efficiturna mollis,cursus tellus euismod,pulvinar sem。 Morbi maximus orci nisi。 Fusce tempor condimentum lacus nec pulvinar。 Aenean tristique eu nibh vitae facilisis。
< / p>
< / body>
< / html>
如果您使用django框架,您可以使用django-easy -pdf。我认为这是从Html生成PDF最不痛苦的方式。这里是我的项目的模板和视图:
pre $ #从easy_pdf.rendering导入easy_pdf渲染
import render_to_pdf_response
#这里是详细视图函数
def detail_to_pdf(request,id):
template ='renderdetail.html'
kucing = Kucing.objects.get(id = id )
context = {'kucing':kucing}
return render_to_pdf_response(request,template,context)
模板是:
{%extendsbase .html%} {%block extra_style%}< style type =text / css> body {font-family:Helvetica,sans-serif;颜色:#333333; }< / style> {%endblock%} {%block content%}< div id =content> < div class =main> < h1> PROFILE:{{kucing.nama}} - ID:{{kucing.id}}< / h1> < img src =/ media / {{kucing.foto}}>< br> < p> Nama:{{kucing.nama}}< / p>< br> < p> Hp:{{kucing.hp}}< / p>< br> < p> Poin:{{kucing.poin}}< / p>< br> < a href ={%url'kucing_makan'kucing.id%}> Makan< / a> < a href ={%url'kucing_berburu'kucing.id%}> Berburu< / a> < HR> < h5>< a href ={%url'kucing_home'%}>返回主页< / a>< / h5> < h5>< a href ={%url'kucing_list'%}>查看另一个Kucing< / a>< / h5> < / DIV> < / div> {%endblock%}
您也可以通过覆盖PDFTemplateViews来使用基于类的视图。您可以在上看到更多信息。
I am trying to convert a html document that I have created into a pdf using report lab. The html document is below. I am unsure on how to do this and I have looked online and cant seem to find a solution for this.
html document
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Convert to Pdf</title>
</head>
<body>
<h2>Convert to pdf</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec at tempus massa. Quisque congue dui venenatis rutrum imperdiet. Nulla congue magna sit amet magna posuere, in elementum felis dapibus. Mauris maximus feugiat lorem, a bibendum orci fringilla a. Pellentesque rhoncus dignissim tempus. Aliquam semper convallis odio ut pharetra. Nunc bibendum neque at bibendum ornare. Curabitur lobortis odio ac turpis tincidunt, at venenatis nibh blandit. Integer id arcu maximus, blandit urna ut, tempor odio. Pellentesque tempus, mi a finibus pellentesque, ex magna lacinia elit, a semper nibh orci non nulla. Nunc felis metus, congue a odio vitae, porttitor pellentesque sem. Fusce vehicula tincidunt dolor at dictum. Integer cursus, risus quis finibus dapibus, nulla dolor dapibus massa, et luctus enim dui a nunc. Sed facilisis sapien at risus commodo, eget sollicitudin ex eleifend. Proin ipsum ipsum, condimentum in mauris vel, rutrum aliquam magna.
Aenean ac odio ante. Proin eget urna est. Fusce at dui dignissim, tincidunt magna eget, dictum nisl. Donec enim ipsum, feugiat a tristique vitae, suscipit non risus. Pellentesque libero leo, pellentesque ut neque ut, pharetra volutpat ex. Pellentesque purus neque, varius eu dolor eu, placerat ullamcorper velit. Etiam volutpat blandit tortor non pellentesque. Donec ac risus lacus. Pellentesque sagittis vitae odio quis vulputate. Praesent efficitur urna mollis, cursus tellus euismod, pulvinar sem. Morbi maximus orci nisi. Fusce tempor condimentum lacus nec pulvinar. Aenean tristique eu nibh vitae facilisis.
</p>
</body>
</html>
If you use django framework you could use django-easy-pdf. I think it's the least painfull way to generate PDF from Html. Here's the template and views of my project:
#Import the easy_pdf rendering
from easy_pdf.rendering import render_to_pdf_response
#Here's the detail view function
def detail_to_pdf(request,id):
template = 'renderdetail.html'
kucing = Kucing.objects.get(id = id)
context = {'kucing' : kucing}
return render_to_pdf_response(request,template,context)
The Template is:
{% extends "base.html" %}
{% block extra_style %}
<style type="text/css">
body {
font-family: "Helvetica", "sans-serif";
color: #333333;
}
</style>
{% endblock %}
{% block content %}
<div id="content">
<div class="main">
<h1>PROFILE : {{ kucing.nama }}- ID:{{ kucing.id }}</h1>
<img src="/media/{{ kucing.foto }}"><br>
<p>Nama : {{ kucing.nama }}</p><br>
<p>Hp : {{ kucing.hp}}</p><br>
<p>Poin : {{ kucing.poin }}</p><br>
<a href="{% url 'kucing_makan' kucing.id %}">Makan</a>
<a href="{% url 'kucing_berburu' kucing.id %}">Berburu</a>
<hr>
<h5><a href="{% url 'kucing_home' %}">Back To Home</a></h5>|
<h5><a href="{% url 'kucing_list' %}">See Another Kucing</a></h5>
</div>
</div>
{% endblock %}
You also able to use Class Based Views by overriding PDFTemplateViews. You can see more on the Docs.
这篇关于如何使用报告实验室与python将html文档转换为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!