Пример #1
0
class TestAddPage(TestCase):
    def setUp(self) -> None:
        self.pdf = Pdf()

    def test_absolute_y(self):
        self.pdf.translate(10, 10)
        self.pdf.add_page()
        actual = self.pdf.absolute_positions
        expected = {1: [10, 10], 2: [0, 0]}
        self.assertEqual(expected, actual)

    def test_draw(self):
        self.pdf.translate_page_margins()
        self.pdf.draw_ruler(mode='h')
        self.pdf.draw_ruler(mode='v')
        self.pdf.translate(10, 10)
        vsl = VerticalSegmentedLine(lengths=[10, 20, 30, 40])
        vsl.draw(self.pdf)
        self.pdf.add_page()
        self.pdf.translate_page_margins()
        self.pdf.draw_ruler(mode='h')
        self.pdf.draw_ruler(mode='v')
        self.pdf.translate(10, 10)
        # print(self.pdf.absolute_positions)
        vsl.draw(self.pdf)
        with self.file_path(path, 'draw', 'pdf') as pdf_path:
            self.pdf.write(pdf_path)

    def test_page_number(self):
        for i in range(3):
            self.pdf.add_page()
        self.pdf.draw_page_numbers(v_position='center', h_position='bottom')

        with self.file_path(path, 'page_number', 'pdf') as pdf_path:
            self.pdf.write(pdf_path)

    def test_draw_in_reversed_order(self):
        self.pdf.translate(30, 30)
        self.pdf.add_page()
        self.pdf.translate(80, 80)
        self.pdf.add_page()
        self.pdf.page = 2
        text = Text('second page')
        text.draw(self.pdf)
        self.pdf.page = 1
        text = Text('first page')
        text.draw(self.pdf)

        for page in self.pdf.pages:
            self.pdf.page = page
            self.pdf.reset_position()
            self.pdf.translate_page_margins()
            self.pdf.draw_ruler('h')
            self.pdf.draw_ruler('v')

        self.pdf.draw_page_numbers(v_position='center', h_position='bottom')

        with self.file_path(path, 'draw_in_reversed_order', 'pdf') as pdf_path:
            self.pdf.write(pdf_path)
Пример #2
0
class TestClip(TestCase):
    def setUp(self) -> None:
        self.pdf = Pdf()

    def test_line(self):
        pdf_path = create_test_path(path, 'line.pdf')
        self.pdf.rect(0, 0, 50, 50)
        self.pdf.clip_rect(0, 0, 50, 50)
        self.pdf.line(10, 20, 100, 100)
        self.pdf.write(pdf_path)
        self.assertCompareFiles(pdf_path)

    def test_line_break(self):
        def draw_with_clip(index):
            with self.pdf.saved_state():
                self.pdf.clip_rect(-1, -5, 196, 50)
                self.pdf.translate(index * -190, 0)
                ruler.draw(self.pdf)

        ruler = HorizontalRuler(length=800, unit=10)
        with self.file_path(path, 'line_break', 'pdf') as pdf_path:
            self.pdf.translate(10, 10)
            number_of_rows = int(ceil(ruler.length / 190))
            for index in range(number_of_rows):
                if index != 0:
                    self.pdf.translate(0, 30)
                draw_with_clip(index)
            self.pdf.write(pdf_path)

    def test_column_line_break(self):
        def draw_with_clip(index):
            with self.pdf.saved_state():
                self.pdf.clip_rect(-1, -5, 196, 50)
                self.pdf.translate(index * -190, 0)
                c.draw(self.pdf)

        c = DrawObjectColumn()
        c.bottom_margin = 10
        c.add_draw_object(HorizontalRuler(length=800))
        c.add_draw_object(HorizontalSegmentedLine(lengths=400 * [2]))
        with self.file_path(path, 'column_line_break', 'pdf') as pdf_path:
            self.pdf.translate_page_margins()
            number_of_rows = int(ceil(c.get_width() / 190))
            for index in range(number_of_rows):
                if index != 0:
                    self.pdf.translate(0, c.get_height())
                draw_with_clip(index)
            self.pdf.write(pdf_path)

    def test_column_page_break(self):
        self.pdf.t_margin = 15

        def _prepare_page():
            self.pdf.translate_page_margins()
            self.pdf.draw_ruler('v')
            self.pdf.draw_ruler('h')
            self.pdf.translate(clip_area_left_margin, clip_area_top_margin)

        def _add_page():
            self.pdf.add_page()
            _prepare_page()

        def draw_with_clip(index):
            with self.pdf.saved_state():
                self.pdf.clip_rect(-1, -5, clip_area_width + 1.14,
                                   clip_area_height)
                self.pdf.translate(index * -clip_area_width, 0)
                c.draw(self.pdf)

        c = DrawObjectColumn()
        c.bottom_margin = 60
        c.add_draw_object(HorizontalRuler(length=1200, bottom_margin=5))
        c.add_draw_object(HorizontalSegmentedLine(lengths=600 * [2]))

        clip_area_left_margin = 10
        clip_area_top_margin = 10
        clip_area_width = self.pdf.w - self.pdf.l_margin - self.pdf.r_margin - clip_area_left_margin
        clip_area_height = c.get_height()

        with self.file_path(path, 'column_page_break', 'pdf') as pdf_path:
            _prepare_page()
            number_of_rows = int(ceil(c.get_width() / clip_area_width))
            for index in range(number_of_rows):
                if index != 0:
                    self.pdf.translate(0, c.get_height())
                if self.pdf.absolute_y > self.pdf.h - self.pdf.b_margin:
                    _add_page()
                draw_with_clip(index)
            self.pdf.write(pdf_path)

    def test_with_clipping_area(self):
        c = DrawObjectColumn()
        c.bottom_margin = 60
        c.add_draw_object(HorizontalRuler(length=1200, bottom_margin=5))
        c.add_draw_object(HorizontalSegmentedLine(lengths=600 * [2]))
        ca = ClippingArea(self.pdf, draw_object=c)
        self.pdf.translate_page_margins()
        ca.draw()
        with self.file_path(path, 'with_clipping_area', 'pdf') as pdf_path:
            self.pdf.write(pdf_path)

    def test_clipped_draw(self):
        c = DrawObjectColumn()
        c.bottom_margin = 60
        c.add_draw_object(HorizontalRuler(length=1200, bottom_margin=5))
        c.add_draw_object(HorizontalSegmentedLine(lengths=600 * [2]))
        self.pdf.translate_page_margins()
        title = PageText('A very nice title',
                         v_position='center',
                         font_weight='bold',
                         font_size=12,
                         top_margin=10)
        title.draw(self.pdf)
        self.pdf.translate(0, title.get_height())
        c.clipped_draw(self.pdf)

        for page in self.pdf.pages:
            self.pdf.page = page
            self.pdf.reset_position()
            self.pdf.translate_page_margins()
            self.pdf.draw_ruler('h')
            self.pdf.draw_ruler('v')
        with self.file_path(path, 'clipped_draw', 'pdf') as pdf_path:
            self.pdf.write(pdf_path)