title: Django HTML & CSS render to pdf
body: <p>I'm trying to convert HTML &amp; CSS into a pdf page using Django-<a href="https://weasyprint.org/" rel="nofollow noreferrer">weasyprint</a> but I'm kind of stuck with their tutorial because I want the PDF to render the current page when the user clicks on the download button and the pdf downloads automatically with read-only privileges. This whole process kind of feels painful to do.</p>
  <p>Currently, weasyprint just converts a URL in Django to pdf, but I don't know how to set the button to look at the weasyprint view.</p>
  <p>Maybe I am looking past it and over complicating it, any assistance would be appreciated.</p>
  <p>Weasyprints example code:</p>
  <pre><code>from django.conf import settings
  from django.views.generic import DetailView
  
  from django_weasyprint import WeasyTemplateResponseMixin
  from django_weasyprint.views import CONTENT_TYPE_PNG
  
  
  class MyModelView(DetailView):
      # vanilla Django DetailView
      model = MyModel
      template_name = 'mymodel.html'
  
  
  class MyModelPrintView(WeasyTemplateResponseMixin, MyModelView):
      # output of MyModelView rendered as PDF with hardcoded CSS
      pdf_stylesheets = [
          settings.STATIC_ROOT + 'css/app.css',
      ]
      # show pdf in-line (default: True, show download dialog)
      pdf_attachment = False
      # suggested filename (is required for attachment!)
      pdf_filename = 'foo.pdf'
  
  
  class MyModelImageView(WeasyTemplateResponseMixin, MyModelView):
      # generate a PNG image instead
      content_type = CONTENT_TYPE_PNG
  
      # dynamically generate filename
      def get_pdf_filename(self):
          return 'bar-{at}.pdf'.format(
              at=timezone.now().strftime('%Y%m%d-%H%M'),
          )
  
  </code></pre>
  <p>I made a virtual env on my pc and it's setup exactly like the example. Currently using Boostrap 4.</p>
  <p>*Edit if there is a better way of doing it, you are more than welcome to share it :)</p>
  <p>Also I want to target just the body tags so that it converts only that section to pdf and not the ENTIRE page.</p>
  <p>The solution I used before this is: <a href="https://codepen.io/AshikNesin/pen/KzgeYX" rel="nofollow noreferrer">https://codepen.io/AshikNesin/pen/KzgeYX</a> but this doesn't work very well.</p>
  <p>*EDIT 2.0</p>
  <p>I've moved on to js and I'm stuck with this script where it doesn't want to create the pdf form on click function also is there a way to set the js function to ONLY download the selected Id in the div and not on certain scale? (afraid that it's going to use the resolution instead of the actual content that needs to be rendered)</p>
  <p><a href="https://jsfiddle.net/u4ko9pzs/18/" rel="nofollow noreferrer">https://jsfiddle.net/u4ko9pzs/18/</a></p>
  <p>Any suggestions would be greatly appreciated.</p>