Ejemplo n.º 1
0
    def apply(self, dest_page: int, x: int, y: int):
        """
        Apply a stamp to a particular page in the PDF writer attached to this
        :class:`.TextStamp` instance.

        :param dest_page:
            Index of the page to which the stamp is to be applied
            (starting at `0`).
        :param x:
            Horizontal position of the stamp's lower left corner on the page.
        :param y:
            Vertical position of the stamp's lower left corner on the page.
        :return:
            A reference to the affected page object, together with
            a ``(width, height)`` tuple describing the dimensions of the stamp.
        """
        stamp_ref = self.register()
        resource_name = b'/Stamp' + hexlify(uuid.uuid4().bytes)
        stamp_paint = b'q 1 0 0 1 %g %g cm %s Do Q' % (rd(x), rd(y),
                                                       resource_name)
        stamp_wrapper_stream = generic.StreamObject(stream_data=stamp_paint)
        resources = generic.DictionaryObject({
            pdf_name('/XObject'):
            generic.DictionaryObject(
                {pdf_name(resource_name.decode('ascii')): stamp_ref})
        })
        wr = self.writer
        page_ref = wr.add_stream_to_page(dest_page,
                                         wr.add_object(stamp_wrapper_stream),
                                         resources)
        dims = (self.box.width, self.box.height)
        return page_ref, dims
Ejemplo n.º 2
0
 def render_command_stream(self):
     # start a command stream with fill colour set to black
     command_stream = ['0 0 0 rg']
     for row, col in self._img:
         (x, y), _ = self.pixel_box(row, col)
         # paint a rectangle
         command_stream.append(
             '%g %g %g %g re f' %
             (rd(x), rd(y), rd(self.box_size), rd(self.box_size)))
     return ' '.join(command_stream).encode('ascii')
Ejemplo n.º 3
0
 def extra_commands(self):
     qr_ref, natural_qr_size = self._qr_xobject()
     self.set_resource(category=ResourceType.XOBJECT,
                       name=pdf_name('/QR'),
                       value=qr_ref)
     height = self.get_stamp_height()
     qr_y_sep = (height - self.qr_size) // 2
     qr_scale = self.qr_size / natural_qr_size
     # paint the QR code, translated and with y axis inverted
     draw_qr_command = b'q %g 0 0 -%g %g %g cm /QR Do Q' % (
         rd(qr_scale),
         rd(qr_scale),
         rd(self.style.innsep),
         rd(height - qr_y_sep),
     )
     return [draw_qr_command]
Ejemplo n.º 4
0
def mm2uu(mm_len):
    return rd((mm_len / 25.4) * PDF_UUPI)