Using Takashi Otaka, "Learning by Moving! Introduction to Python Django Development" I'm a beginner who started learning python and Django.
I've been studying programming for about 4 months using ruby / rails, When I changed jobs as an engineer, I decided to use python, so I started studying with this book in my hand.
Since I am a de-class amateur, I would be very grateful if you could comment on supplements and suggestions.
I want to convert an HTML page to PDF and display it. I also want to create a graph with'matplotlib'and put it in PDF as an image.
HTML↓
from django.shortcuts import render
def html_view(request):
.
.
.
#Fill variables into context and pass to HTML
context = {
"hoge": hoge,
"fuga": fuga,
}
return render(request, 'hogefuga.html', context)
PDF conversion ↓
from django.http import HttpResponse
from django.shortcuts import render
from weasyprint import HTML, CSS
from django.template.loader import get_template
def pdf_view(request):
.
.
.
#Specify template & pass variables
html_template = get_template('hogefuga.html')
html_str = html_template.render({
"hoge": hoge,
"fuga": fuga,
}, request)
#PDF conversion
pdf_file = HTML(string=html_str, encoding='utf-8').write_pdf(
stylesheets=[
#WeasyPrint will not be applied unless you specify the CSS file here.
CSS('./static/css/hogefuga.css'),
],
)
response = HttpResponse(pdf_file, content_type='application/pdf')
response['Content-Disposition'] = 'filename="hogefuga.pdf"'
return response
The wisdom of our ancestors is amazing. I managed to struggle so far ...
Now, let's create a graph as well.
from django.http import HttpRespons
from django.shortcuts import render
from weasyprint import HTML, CSS
from django.template.loader import get_template
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.use('Agg')
def pdf_graph_view(request):
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
fig = plt.figure(figsize=(4.0, 3.0))
ax = fig.add_subplot(111)
ax.plot(x, y)
plt.savefig('static/img/graph.png', format='png', dpi=200)
.
.
.
Same as PDF code below
By the way, if the following description is not provided, an error will occur. I've been deprived of time by just this description ... Click here for the wisdom of the borrowed ancestor
mpl.use('Agg')
Well, it's over by writing the location of the image file in HTML.
hogefuga.html
<img src="{% static 'img/graph.png' %}">
However, the wholesaler does not wholesale it. For some reason, the image is not displayed in the PDF. Why.
pdf_file = HTML(string=html_str, base_url=request.build_absolute_uri(), encoding='utf-8')
change to. Then, the graph is displayed, but the characters are garbled. It seems that the PC will finally be destroyed. goodbye Jobs.
If the characters are garbled for the time being, There is no choice but to raise the priority of fonts by force. (Brain dead single cell)
hogefuga.css
font-family: 'msmincho'!important;
!! If you add important, the garbled characters will be solved safely. ~~ However, it is not a fundamental solution. Cover the ugly things. ~~
The ancestors are amazing. The technician is amazing. Longing for. It seems that it will take time to get out of the beginners. Studying every day, isn't it? Why are the characters garbled (CSS is not working)? I have to solve it.
・ Matplotlib reverse lookup memo (frame edition) ・ Tips for drawing with matplotlib on the server side -Easy PDF output with Django2.0 + WeasyPrint · PDF output using Weasyprint not showing images (Django)
Recommended Posts