docs: def write_pdf(matrix, version, out, scale=1, border=None, color='#000',
                background=None, compresslevel=9):
      """\
      Serializes the QR Code as PDF document.
  
      :param matrix: The matrix to serialize.
      :param int version: The (Micro) QR code version
      :param out: Filename or a file-like object supporting to write bytes.
      :param scale: Indicates the size of a single module (default: 1 which
              corresponds to 1 x 1 pixel per module).
      :param int border: Integer indicating the size of the quiet zone.
              If set to ``None`` (default), the recommended border size
              will be used (``4`` for QR Codes, ``2`` for a Micro QR Codes).
      :param color: Color of the modules (default: black). The
              color can be provided as ``(R, G, B)`` tuple, as web color name
              (like "red") or in hexadecimal format (``#RGB`` or ``#RRGGBB``).
      :param background: Optional background color (default: ``None`` = no
              background color). See `color` for valid values.
      :param int compresslevel: Integer indicating the compression level
              (default: 9). 1 is fastest and produces the least
              compression, 9 is slowest and produces the most.
              0 is no compression.
      """
  
      def write_string(writemeth, s):
          writemeth(s.encode('ascii'))
  
      def to_pdf_color(clr):
          """\
          Converts the provided color into an acceptable format for PDF's
          "DeviceRGB" color space.
           """
          def to_float(c):
              if isinstance(c, float):
                  if not 0.0 <= c <= 1.0:
                      raise ValueError('Invalid color "{0}". Not in range 0 .. 1'
                                       .format(c))
                  return c
              return 1 / 255.0 * c if c != 1 else c
          return tuple([to_float(i) for i in colors.color_to_rgb(clr)])
  
      check_valid_scale(scale)
      check_valid_border(border)
      width, height = get_symbol_size(version, scale, border)
      border = get_border(version, border)
      creation_date = "{0}{1:+03d}'{2:02d}'".format(time.strftime('%Y%m%d%H%M%S'),
                                                    time.timezone // 3600,
                                                    abs(time.timezone) % 60)
      cmds = []
      append_cmd = cmds.append
      if scale > 1:
          append_cmd('{0} 0 0 {0} 0 0 cm'.format(scale))
      if background is not None:
          # If the background color is defined, a rect is drawn in the background
          append_cmd('{} {} {} rg'.format(*to_pdf_color(background)))
          append_cmd('0 0 {} {} re'.format(width, height))
          append_cmd('f q')
      # Set the stroke color only iff it is not black (default)
      if not colors.color_is_black(color):
          append_cmd('{} {} {} RG'.format(*to_pdf_color(color)))
      # Current pen position y-axis
      # Note: 0, 0 = lower left corner in PDF coordinate system
      y = get_symbol_size(version, scale=1, border=0)[1] + border - .5
      # Set the origin in the upper left corner
      append_cmd('1 0 0 1 {0} {1} cm'.format(border, y))
      # PDF supports absolute coordinates, only
      for (x1, y1), (x2, y2) in matrix_to_lines(matrix, 0, 0, incby=-1):
          append_cmd('{0} {1} m {2} {1} l'.format(x1, y1, x2, y2))
      append_cmd('S')
      graphic = zlib.compress((' '.join(cmds)).encode('ascii'), compresslevel)
      with writable(out, 'wb') as f:
          write = f.write
          writestr = partial(write_string, write)
          object_pos = []
          write(b'%PDF-1.4\r%\xE2\xE3\xCF\xD3\r\n')
          for obj in ('obj <</Type /Catalog /Pages 2 0 R>>\r\nendobj\r\n',
                      'obj <</Type /Pages /Kids [3 0 R] /Count 1>>\r\nendobj\r\n',
                      'obj <</Type /Page /Parent 2 0 R /MediaBox [0 0 {0} {1}] /Contents 4 0 R>>\r\nendobj\r\n'.format(width, height),
                      'obj <</Length {0} /Filter /FlateDecode>>\r\nstream\r\n'.format(len(graphic))):
              object_pos.append(f.tell())
              writestr('{0} 0 {1}'.format(len(object_pos), obj))
          write(graphic)
          write(b'\r\nendstream\r\nendobj\r\n')
          object_pos.append(f.tell())
          writestr('{0} 0 obj <</CreationDate(D:{1})/Producer({2})/Creator({2})\r\n>>\r\nendofbj\r\n' \
                   .format(len(object_pos), creation_date, CREATOR))
          object_pos.append(f.tell())
          xref_location = f.tell()
          writestr('xref\r\n0 {0}\r\n0000000000 65535 f\r\n'.format(len(object_pos) + 1))
          for pos in object_pos:
              writestr('{0:010d} {1:05d} n\r\n'.format(pos, 0))
          writestr('trailer <</Size {0}/Root 1 0 R/Info 5 0 R>>\r\n'.format(len(object_pos) + 1))
          writestr('startxref\r\n{0}\r\n%%EOF\r\n'.format(xref_location))