def test_box_constraint_recalc(): w = 1600 h = 900 ar = Fraction(16, 9) bc = BoxConstraints(aspect_ratio=ar) assert bc.aspect_ratio == ar with pytest.raises(BoxSpecificationError): # noinspection PyStatementEffect bc.height with pytest.raises(BoxSpecificationError): # noinspection PyStatementEffect bc.width bc.width = w assert bc.height_defined assert bc.height == h bc = BoxConstraints(aspect_ratio=ar) bc.height = h assert bc.width_defined assert bc.width == w bc = BoxConstraints(width=w) with pytest.raises(BoxSpecificationError): # noinspection PyStatementEffect bc.aspect_ratio bc.height = h assert bc.aspect_ratio == ar
def apply(self, sig_annot, writer): """ Apply the settings to an annotation. .. danger:: This method is internal API. """ try: x1, y1, x2, y2 = sig_annot['/Rect'] except KeyError: return w = abs(x1 - x2) h = abs(y1 - y2) if w and h: # the field is probably a visible one, so we change its appearance # stream to show some data about the signature stamp = self._appearance_stamp(writer, BoxConstraints(width=w, height=h)) sig_annot['/AP'] = stamp.as_appearances().as_pdf_object() try: # if there was an entry like this, it's meaningless now del sig_annot[pdf_name('/AS')] except KeyError: pass
def _init_text_box(self): # if necessary, try to adjust the text box's bounding box # to the stamp's box = self.box expected_w = None if box.width_defined: expected_w = box.width - self.text_box_x() self.expected_text_width = expected_w expected_h = None if box.height_defined: expected_h = box.height - self.text_box_y() box = None if expected_h and expected_w: # text boxes do not auto-scale their font size, so # we have to take care of that box = BoxConstraints( aspect_ratio=Fraction(int(expected_w), int(expected_h))) self.text_box = TextBox(self.style.text_box_style, writer=self.writer, resources=self.resources, box=box)
def test_qr_fixed_size(): writer = IncrementalPdfFileWriter(BytesIO(MINIMAL)) w = 280 h = 60 qrss = stamp.QRStampStyle() box = BoxConstraints(width=w, height=h) qr = stamp.QRStamp(writer, 'https://example.com', qrss, box=box) qr.as_form_xobject() qr.apply(0, 10, 10) assert qr.text_box.box.width == 224
def set_text_field_in_group(writer, ix, val): tf_parent = writer.root['/AcroForm']['/Fields'][1].get_object() tf = tf_parent['/Kids'][ix].get_object() appearance = RawContent(box=BoxConstraints(height=60, width=130), data=b'''q 0 0 1 rg BT /Ti 12 Tf (%s) Tj ET Q''' % val.encode('ascii')) tf['/V'] = generic.pdf_string(val) tf['/AP'] = generic.DictionaryObject({ generic.pdf_name('/N'): writer.add_object(appearance.as_form_xobject()) }) writer.update_container(tf)
def test_simple_textbox_render(with_border, natural_size): tbs = text.TextBoxStyle(border_width=1 if with_border else 0) bc = None if natural_size else BoxConstraints(width=1600, height=900) textbox = text.TextBox(style=tbs, box=bc) textbox.content = 'This is a textbox with some text.\nAnd multiple lines' xobj = textbox.as_form_xobject() x1, y1, x2, y2 = xobj['/BBox'] assert '/F1' in textbox.resources.font if not natural_size: assert abs(x1 - x2) == 1600 assert abs(y1 - y2) == 900
def __init__(self, barcode_type, code): self.barcode_type = barcode_type self.code = code writer = PdfStreamBarcodeWriter() # render everything here, since we need part of the rendering # operation's results to determine the box parameters to pass to # the parent b = barcode.get_barcode(self.barcode_type, code=code, writer=writer) self._barcode_commands = b.render() (w, h) = writer.size super().__init__(box=BoxConstraints(width=w, height=h))
def test_box_constraint_over_underspecify(): w = 1600 h = 900 ar = Fraction(16, 9) with pytest.raises(BoxSpecificationError): BoxConstraints(width=w, height=h, aspect_ratio=ar) bc = BoxConstraints(width=w) assert not bc.aspect_ratio_defined assert not bc.height_defined bc = BoxConstraints(width=w, height=h) assert bc.aspect_ratio == ar bc = BoxConstraints(width=w, aspect_ratio=ar) assert bc.height == h with pytest.raises(BoxSpecificationError): bc.height += 1 bc = BoxConstraints(height=h, aspect_ratio=ar) assert bc.width == w with pytest.raises(BoxSpecificationError): bc.width += 1 bc = BoxConstraints() bc.width = w assert bc.width_defined assert not bc.height_defined assert not bc.aspect_ratio_defined bc.height = h assert bc.aspect_ratio == ar
:param text_params: Additional parameters for text template interpolation. """ _stamp_file(input_name, output_name, style, QRStamp, dest_page, x, y, url=url, text_params=text_params) STAMP_ART_CONTENT = RawContent(box=BoxConstraints(width=100, height=100), data=b''' q 1 0 0 -1 0 100 cm 0.603922 0.345098 0.54902 rg 3.699 65.215 m 3.699 65.215 2.375 57.277 7.668 51.984 c 12.957 46.695 27.512 49.34 39.418 41.402 c 39.418 41.402 31.48 40.078 32.801 33.465 c 34.125 26.852 39.418 28.172 39.418 24.203 c 39.418 20.234 30.156 17.59 30.156 14.945 c 30.156 12.297 28.465 1.715 50 1.715 c 71.535 1.715 69.844 12.297 69.844 14.945 c 69.844 17.59 60.582 20.234 60.582 24.203 c 60.582 28.172 65.875 26.852 67.199 33.465 c 68.52 40.078 60.582 41.402 60.582 41.402 c 72.488 49.34 87.043 46.695 92.332 51.984 c 97.625 57.277 96.301 65.215 96.301 65.215 c h f 3.801 68.734 92.398 7.391 re f 3.801 79.512 92.398 7.391 re f 3.801 90.289 92.398 7.391 re f Q