title: PDF print to Sticker/Label Thermal Printer [PYTHON]
body: <p>I need help in figuring how to print pdfs to sticker/label printers properly.
  One-Sticker-Per-Page Printers are working fine currently though printers with multiple labels per row do not work as expected.
  Here is my code to generate PDF files:</p>
  <pre class="lang-py prettyprint-override"><code>
  from PIL import Image
  import PyPDF2
  
  img = Image.open('somelabelimage.png', 'r')  # load the image
  
  # calculate pixels from mm
  def gpm(mm):
      return round((72 * mm) / 25.4)
  
  img = img.resize(
      (
         gpm(15)*10,gpm(5)*10 # 15mm X 5mm
      ),
      Image.LANCZOS
  )
  
  
  img_w, img_h = img.size
  
  # create empty new PIL.Image
  pivot = Image.new('RGB', ( 4*(img_w+ (gpm(4)*10))-(gpm(4)*10), img_h), (255, 255, 255))
  x = 0
  
  # here we paste each label-image onto the new surface
  for y in range(4):
      pivot.paste(img,(x,0))
      x += img_w + (gpm(4)*10) # 4 mm offset increased by factor 10 
  
  # save the image as pdf
  pivot.save(&quot;new.pdf&quot;,resolution=200)
  
  # code below resizes the pdf file to fit the mm specs
  pr = PyPDF2.PdfFileReader(open('new.pdf','rb'))
  pw = PyPDF2.PdfFileWriter()
  
  def mti(mm):
      return mm / 25.4
  
  page = pr.getPage(0)
  page.scaleTo(mti(15)*72*4,mti(5)*72)
  
  pw.addPage(page)
  pw.write(open(&quot;resized.pdf&quot;,'wb'))
  </code></pre>
  <p>So this is how I actually print:</p>
  <pre class="lang-py prettyprint-override"><code>import subprocess
  
  # pdftopriter.exe is an exe that lets me print to printers. Here is the link to theri page : # http://www.columbia.edu/~em36/pdftoprinter.html
  command = [
    &quot;session/PDFtoPrinter.exe&quot;, &quot;storage/storage_export.pdf&quot;,
    self.printer if self.printer is not None else &quot;None&quot;
  ]
  
  subprocess.Popen(command)
  </code></pre>
  <p>Result for single-labels:
  <a href="https://i.stack.imgur.com/rTXVN.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rTXVN.jpg" alt="enter image description here" /></a></p>
  <p>So they are workig fine however here is the result for the other printer:
  <a href="https://i.stack.imgur.com/zIMsd.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zIMsd.jpg" alt="enter image description here" /></a></p>
  <p>As you can see this print job is prett off. But I cant figure how to fix/adjust that.</p>
  <br>
  I appreciate every kind of help or advice regarding pdf creation or printing in general.
  If you found a better way to print images please help me out!
  Thanks in advance!