1

I have already made html file template in the frontend (ReactNative Expo, I would've used a library to convert the html to pdf, but expo doesn't provide one), and by sending it to the backend I want it to be converted and saved as pdf,

I'm newbie in django but I did tried some searching, here's what I found : https://www.codingforentrepreneurs.com/blog/html-template-to-pdf-in-django/ , If I go with this approach, it will be unnecessary to get multiple data from the user, since the form is very detailed, and frontend is enough to fill. I also did this approach but I don't know what I am missing (https://www.codingforentrepreneurs.com/blog/save-a-auto-generated-pdf-file-django-model) , here is my code so far:

I expect the html file to be auto converted to pdf, but it still saving it as html.

UPDATE :

(models.py)

class Questionary(models.Model):
    date = models.DateField(auto_now_add=True)
    title = models.CharField(max_length = 100)
    file = models.FileField(upload_to='Documents/%Y/%m/%d/', blank = 
           False, 
            null = False)
    def generate_obj_pdf(self):
        this = Questionary.objects.get(id=self.id)
        render_to_pdf(this.file)

(utils.py)

from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template
import pdfkit
from xhtml2pdf import pisa
  def render_to_pdf(your_template):
    template = get_template(your_template.html)
    response = HttpResponse(pdfkit.from_string(html), 
               content_type='application/pdf')
    return response

(views.py)

    class CreateQuestionaryAPIView(CreateAPIView):
        serializer_class = CreateQuestionarySerializer

(serializer.py)

class CreateQuestionarySerializer(serializers.ModelSerializer):
    class Meta:
        model = Questionary
        fields = '__all__'

1 Answer 1

4

I use pdfkit when converting dynamic django templates to pdf's, it's very easy to use. You need to use it such as:

import pdfkit

from django.template.loader import get_template


def render_to_pdf():
    # prepare your context for html template, like you do for django templates
    template = get_template('your_template.html')
    html = template.render(context=context)
    return pdfkit.from_string(html)
Sign up to request clarification or add additional context in comments.

4 Comments

Where should I use this code? like under my serializer where it is created, or under the model itself? I also found this line useful pdfkit.from_file('file.html', 'out.pdf') . I don't need to make another template as I already have an html file (from the user), I just want it to be converted to pdf, is there a possibility to it?
Place this under your utils.py or helper.py is more convenient and yes you can use the html file directly in input.
I still got no luck on converting, the output is still in html file : "file": "http://localhost:8000/media/Documents/2019/Questions_L93qhKk.html"
change last line to response = HttpResponse(pdfkit.from_string(html), content_type='application/pdf')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.