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__'