title: Django Render HTML to PDF
body: <p>I'm trying to render an html file to pdf however while the pdf functionality is working fine the rendering is working properly, no css. Also it renders the whole page, how can i render specific sections of the html page. I'm using xhtml2pdf.</p>
  <p>views.py file</p>
  <pre><code>from io import BytesIO
  from django.http import HttpResponse
  from django.template.loader import get_template
  from django.views import View
  from xhtml2pdf import pisa
  
  def render_to_pdf(template_src, context_dict={}):
      template = get_template(template_src)
      html  = template.render(context_dict)
      result = BytesIO()
      pdf = pisa.pisaDocument(BytesIO(html.encode(&quot;utf-8&quot;)), result)
      if not pdf.err:
          return HttpResponse(result.getvalue(), content_type='application/pdf')
      return None
  
  data = {
      &quot;company&quot;: &quot;Name&quot;,
      }
  
  
  #Opens up page as PDF
  class ViewPDF(View):
      def get(self, request, *args, **kwargs):
  
          pdf = render_to_pdf('listings/listing.html', data)
          return HttpResponse(pdf, content_type='application/pdf')
  </code></pre>
  <p><strong>urls.py file</strong></p>
  <pre><code>path('pdf_view/', views.ViewPDF.as_view(), name=&quot;pdf_view&quot;),
  </code></pre>
  <p><strong>html file</strong></p>
  <pre><code>&lt;section class=&quot;p0&quot;&gt;
          &lt;div class=&quot;container&quot;&gt;
              &lt;div class=&quot;row listing_single_row&quot;&gt;
                  &lt;div class=&quot;col-sm-6 col-lg-7 col-xl-8&quot;&gt;
                      &lt;div class=&quot;single_property_title&quot;&gt;
                          &lt;a href=&quot;{{ listing.photo_1.url }}&quot; class=&quot;upload_btn popup-img&quot;&gt;&lt;span class=&quot;flaticon-photo-camera&quot;&gt;&lt;/span&gt; View Photos&lt;/a&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;col-sm-6 col-lg-5 col-xl-4&quot;&gt;
                      &lt;div class=&quot;single_property_social_share&quot;&gt;
                          &lt;div class=&quot;spss style2 mt10 text-right tal-400&quot;&gt;
                              &lt;ul class=&quot;mb0&quot;&gt;
                                  &lt;li class=&quot;list-inline-item&quot;&gt;&lt;a href=&quot;#&quot;&gt;&lt;span class=&quot;flaticon-transfer-1&quot;&gt;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
                                  &lt;li class=&quot;list-inline-item&quot;&gt;&lt;a href=&quot;#&quot;&gt;&lt;span class=&quot;flaticon-heart&quot;&gt;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
                                  &lt;li class=&quot;list-inline-item&quot;&gt;&lt;a href=&quot;#&quot;&gt;&lt;span class=&quot;flaticon-share&quot;&gt;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
                                  &lt;li class=&quot;list-inline-item&quot;&gt;&lt;a href=&quot;{% url 'pdf_view' %}&quot;&gt;&lt;span class=&quot;flaticon-printer&quot;&gt;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
                              &lt;/ul&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/section&gt;
  </code></pre>
  <p>Thank you!</p>