I have a Blog in wordpress and I'm migrating Django, I have a plugin installed in wordpress that allows you to generate a PDF of a post and I want to pass that functionality to Django.
After a lot of searching and several tests, I use django-wkhtmltopdf
to generate the PDF, but I can not generate it correctly.
I have integrated CKeditor to generate the content of the posts that are stored in the field body
#models.py
from ckeditor.fields import RichTextField
from ckeditor_uploader.fields import RichTextUploadingField
class Entry(models.Model):
title = models.CharField(max_length=200)
body = RichTextUploadingField()
slug = models.SlugField(max_length=200, unique=True)
.....
I use PDFTemplateResponse
in a CBV view for PDF generation
#views.py
class MyPDFView(DetailView):
model = Entry
template = 'pdf_export.html'
context = {'titulo': 'Hola prueba'}
def get(self, request, *args, **kwargs):
self.context['entry'] = self.get_object()
response=PDFTemplateResponse(request=request,
template=self.template,
filename ="postPDF.pdf",
context=self.context,
show_content_in_browser=True,
cmd_options={'margin-top': 50,}
)
return response
To call the view I use the following urls
url(r'^pdf/(?P<slug>\S+)$', views.MyPDFView.as_view(), name='pagina_detalle'),
When I generate a PDF I do it with the following template.
<!DOCTYPE html>
<body>
<h1> {{ titulo }}</h1>
<p>{{ entry.title }}</p>
<br/>
<p>{{entry.body}}</p>
</body>
Finally, I created a link in each post to generate the PDF, which calls the corresponding url.
<i><u><h5><a href="{% url "pagina_detalle" slug=object.slug %}"> Generar PDF de {{object.title}}</a></h5></u></i>
All this generates a pdf with title, post title, title
, and content, body
.
The problem is with the field body
, it shows all the HTML code that CKeditor generates, it also does not show the images of a post.
How can I generate a PDF correctly?