title: How to remove margins from PDF? (Generated using WeasyPrint)
body: <p>I am trying to render a PDF document within my Flask application. For this, I am using the following HTML template:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
  &lt;head&gt;
  &lt;style&gt;
      @page {
          margin:0
      }
      h1 {
          color:white;
      }
      .header{
          background: #0a0045;
          height: 250px;
      }
      .center {
          position: relative;
          top: 50%;
          left: 50%;
          -ms-transform: translate(-50%, -50%);
          transform: translate(-50%, -50%);
          text-align:center;
      }
  
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div class="header"&gt;
      &lt;div class="center"&gt;
          &lt;h1&gt;Name&lt;/h1&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I keep getting white margins at the top and right/left of my header section:</p>
  
  <p><a href="https://i.stack.imgur.com/IgAT7.png" rel="noreferrer"><img src="https://i.stack.imgur.com/IgAT7.png" alt="How to remove margins from this PDF ?"></a></p>
  
  <p>Is there a way to remove them?</p>
  
  <p>Edit:</p>
  
  <p>Below is the code used to generate the PDF file using WeasyPrint in my Flask app:</p>
  
  <pre><code>def generate_pdf(id):
      element = Element.query.filter_by(id=id).first()
      attri_dict = get_element_attri_dict_for_tpl(element)
      html = render_template('element.html', attri_dict=attri_dict)
      pdf = HTML(string=html).write_pdf()
      destin_loc = app.config['ELEMENTS_FOLDER']
      timestamp = dt.datetime.now().strftime('%Y%m%d%H%M%S')
      file_name = '_'.join(['new_element', timestamp])
      path_to_new_file = destin_loc + '/%s.pdf' % file_name
      f = open(path_to_new_file, 'wb')
      f.write(pdf)
      filename = return_latest_element_path()
      return send_from_directory(directory=app.config['ELEMENTS_FOLDER'],
                             filename=filename,
                             as_attachment=True)
  </code></pre>