def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) ChunkOfText( "é", font_size=Decimal(24), ).layout( page, Rectangle(Decimal(100), Decimal(600), Decimal(100), Decimal(100))) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def _test_document(self, file): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / (file.stem + "_out.pdf") # attempt to read PDF doc_a = None with open(file, "rb") as in_file_handle: print("\treading (1) ..") doc_a = PDF.loads(in_file_handle) # attempt to read PDF with open(self.input_file_b, "rb") as in_file_handle_b: print("\treading (2) ..") doc_b = PDF.loads(in_file_handle_b) # concat all pages to same document doc_c = Document() for i in range(0, int(doc_a.get_document_info().get_number_of_pages())): doc_c.append_page(doc_a.get_page(i)) for i in range(0, int(doc_b.get_document_info().get_number_of_pages())): doc_c.append_page(doc_b.get_page(i)) # attempt to store PDF with open(out_file, "wb") as out_file_handle: print("\twrite ..") PDF.dumps(out_file_handle, doc_c) return True
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) for i in range(0, 10): space = "".join([" " for _ in range(0, i + 1)]) txt = "".join([ x + space for x in ["apple", "banana", "carrot", "dragonfruit"] ]) layout.add(Paragraph(txt, respect_spaces_in_text=True)) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) ul = OrderedList() ul.add(Paragraph(text="Lorem Ipsum Dolor Sit Amet Consectetur Nunc")) ul.add(Paragraph(text="Ipsum")) ul.add(Paragraph(text="Dolor")) ul.add(Paragraph(text="Sit")) ul.add(Paragraph(text="Amet")) layout = SingleColumnLayout(page) layout.add(ul) # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) Paragraph( "Once upon a midnight dreary, while I pondered weak and weary, over many a quaint and curious volume of forgotten lore", font_size=Decimal(20), horizontal_alignment=Alignment.RIGHT, ).layout( page, Rectangle(Decimal(20), Decimal(600), Decimal(500), Decimal(124)), ) # add rectangle annotation page.append_square_annotation( stroke_color=X11Color("Red"), rectangle=Rectangle(Decimal(20), Decimal(600), Decimal(500), Decimal(124)), ) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): sentences = [ "THE BOAT WILL ARRIVE ON MONDAY", "SHE LIVES AT THE HOUSE WITH THE BLUE DOOR", "A FRIEND IN NEED IS A FRIEND INDEED", "AN APPLE A DAY KEEPS THE DOCTOR AWAY", ] pdf = Document() page = Page() pdf.append_page(page) # layout layout = SingleColumnLayout(page) # add title layout.add( Paragraph( "Secret Code Puzzle", font_size=Decimal(20), font_color=X11Color("YellowGreen"), ) ) # add text layout.add( Paragraph( """ There are three riddles below written in a secret code. Can you unravel them? Once you have, copy the sentence on the line underneath the code. Hint: Each letter of the alphabet has been replaced by a number. All the riddles use the same code. """, respect_newlines_in_text=True, font_color=X11Color("SlateGray"), font_size=Decimal(8), ) ) # add grid for s in sentences: layout.add(self._build_table_for_sentence(s)) layout.add(Paragraph(" ", respect_spaces_in_text=True)) # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) # title layout.add( Paragraph( "Lissajours Line Art", font_size=Decimal(20), font_color=X11Color("Blue"), )) # table N = 7 fill_colors = [ HSVColor(Decimal(x / N), Decimal(1), Decimal(1)) for x in range(0, N) ] stroke_colors = [HSVColor.darker(x) for x in fill_colors] fixed_bb = Rectangle(Decimal(0), Decimal(0), Decimal(100), Decimal(100)) t = Table(number_of_rows=N, number_of_columns=N) for i in range(0, N): for j in range(0, N): t.add( Shape( LineArtFactory.lissajours(fixed_bb, i + 1, j + 1), fill_color=fill_colors[(i + j) % N], stroke_color=stroke_colors[(i + j) % N], line_width=Decimal(2), )) t.set_padding_on_all_cells(Decimal(10), Decimal(10), Decimal(10), Decimal(10)) layout.add(t) # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) # write title layout.add( Paragraph("Nonogram", font_size=Decimal(20), font_color=X11Color("YellowGreen"))) # write text layout.add( Paragraph( """ Nonograms, also known as Paint by Numbers, Picross, Griddlers, Pic-a-Pix, and various other names, are picture logic puzzles in which cells in a grid must be colored or left blank according to numbers at the side of the grid to reveal a hidden picture. In this puzzle type, the numbers are a form of discrete tomography that measures how many unbroken lines of filled-in squares there are in any given row or column. For example, a clue of "4 8 3" would mean there are sets of four, eight, and three filled squares, in that order, with at least one blank square between successive sets. """, font_color=X11Color("SlateGray"), font_size=Decimal(8), )) # write nonogram ng = Nonogram( # "https://i.pinimg.com/originals/f8/23/88/f823882e7c5fa42790e78f43ecf7e8bf.jpg" "https://cdn.shopify.com/s/files/1/2123/8425/products/166422700-LRG_242a4c8b-cad5-476e-afd1-c8b882d48fc2_530x.jpg" ) layout.add(ng) # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) # generate maze m = Maze(20, 20) # add layout = SingleColumnLayout(page) # add title layout.add( Paragraph( "Square Maze", font_size=Decimal(20), font_color=HexColor("274029"), )) # add subtitle layout.add( Paragraph( """ Can you solve this maze? Try going from (lower) left to (upper) right. Good luck """, respect_newlines_in_text=True, )) # add maze layout.add( DisjointShape( m.get_walls(Decimal(10)), stroke_color=HexColor("315C2B"), line_width=Decimal(1), horizontal_alignment=Alignment.CENTERED, vertical_alignment=Alignment.MIDDLE, )) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): pdf = Document() page = Page() pdf.append_page(page) # layout layout = MultiColumnLayout(page, number_of_columns=2) # # add title layout.add( Paragraph( """ Help the person find their way home by colouring in a path through the stepping stones by making a sentence. A really fun way to practice the alphabet! """, font_color=X11Color("SlateGray"), font_size=Decimal(8), )) seq = [x for x in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"] layout.add( SteppingStonePuzzle( seq, seq, "https://icons.iconarchive.com/icons/chanut/role-playing/128/King-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Castle-icon.png", )) # go to next column layout.switch_to_next_column() # add title layout.add( Paragraph( "Stepping Stones", font_size=Decimal(20), font_color=X11Color("YellowGreen"), )) # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) rs = [] for i, s in enumerate( [ "Once upon a midnight dreary,", "while I pondered weak and weary,", "over many a quaint and curious", "volume of forgotten lore", ] ): r = LineOfText( s, font_size=Decimal(20), horizontal_alignment=Alignment.CENTERED, ).layout( page, Rectangle( Decimal(20), Decimal(724 - 24 * i), Decimal(500), Decimal(24) ), ) rs.append(r) # add rectangle annotation page.append_square_annotation( stroke_color=X11Color("Salmon"), rectangle=Rectangle( Decimal(20), Decimal(724 - 24 * 3), Decimal(500), Decimal(24 * 4) ), ) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def _write_maze_page(self, pdf: Document, maze_url: str, title_color: str): # add page page = Page() pdf.append_page(page) # generate maze m = SilhouetteMaze(maze_url) # add layout = SingleColumnLayout(page) # add title layout.add( Paragraph( "MAZE NR %d" % (page.get_page_info().get_page_number() + 1), font="TimesRoman", font_size=Decimal(20), font_color=HexColor(title_color), ) ) # add subtitle layout.add( Paragraph( """ Can you solve this maze? Try going from (lower) left to (upper) right. Good luck """, respect_newlines_in_text=True, font_color=X11Color("SlateGray"), font_size=Decimal(8), ) ) # add maze layout.add( DisjointShape( m.get_walls(Decimal(10)), stroke_color=HexColor(title_color), line_width=Decimal(1), horizontal_alignment=Alignment.CENTERED, vertical_alignment=Alignment.MIDDLE, ) )
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) bb = Rectangle(Decimal(20), Decimal(600), Decimal(500), Decimal(124)) Paragraph( "Once upon a midnight dreary, while I pondered weak and weary, over many a quaint and curious volume of forgotten lore", font_size=Decimal(20), vertical_alignment=Alignment.TOP, horizontal_alignment=Alignment.LEFT, padding_top=Decimal(5), padding_right=Decimal(5), padding_bottom=Decimal(5), padding_left=Decimal(5), ).layout( page, bb, ) # add rectangle annotation page.append_square_annotation( stroke_color=X11Color("Red"), rectangle=bb, ) # determine output location out_file_001 = self.output_dir / "output_001.pdf" out_file_002 = self.output_dir / "output_002.pdf" # attempt to store PDF with open(out_file_001, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) print("\nSAVING SECOND TIME\n") with open(out_file_002, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) # layout layout = SingleColumnLayout(page) layout.add( Paragraph( "pText now support codeblock style paragraphs:", font_color=X11Color("YellowGreen"), font_size=Decimal(20), )) # read self with open(__file__, "r") as self_file_handle: file_contents = self_file_handle.read() layout.add(CodeBlock( file_contents, font_size=Decimal(5), )) layout.add( Paragraph( "By default, these LayoutElements are first formatted by black. " "The font is Courier, and the background and font_color are adjusted as well.", font_size=Decimal(8), )) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) N = 4 colors = [ HSVColor(Decimal(x / 360.0), Decimal(1), Decimal(1)) for x in range(0, 360, int(360 / N)) ] t = Table(number_of_rows=N, number_of_columns=N) for i in range(0, N): for _ in range(0, N): t.add( TableCell( Shape( points=BlobFactory.blob(i + 3), stroke_color=colors[i], fill_color=None, line_width=Decimal(1), horizontal_alignment=Alignment.CENTERED, ).scale_up(Decimal(100), Decimal(100)))) t.set_padding_on_all_cells(Decimal(2), Decimal(2), Decimal(2), Decimal(2)) layout.add(t) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = MultiColumnLayout(page, number_of_columns=2) layout.add(Heading("The Raven", font_size=Decimal(20))) layout.add( Paragraph( "Edgar Allen Poe", font="Helvetica-Oblique", font_size=Decimal(8), font_color=X11Color("SteelBlue"), ) ) for i in range(0, 100): layout.add( Heading("Heading %d" % i, font_size=Decimal(20), outline_level=1) ) for _ in range(0, random.choice([10, 20, 3])): layout.add( Paragraph( "Once upon a midnight dreary, while I pondered, weak and weary, Over many a quaint and curious volume of forgotten lore- While I nodded, nearly napping, suddenly there came a tapping, As of some one gently rapping, rapping at my chamber door. Tis some visitor, I muttered, tapping at my chamber door- Only this and nothing more.", font_size=Decimal(12), font_color=X11Color("SlateGray"), horizontal_alignment=Alignment.LEFT, ) ) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_type_methods(self): # first document d0 = Document() p0 = Page() d0.append_page(p0) # second document d1 = Document() p1 = Page() d1.append_page(p1) # add listener to d0 d0.add_event_listener(SVGExport()) # check other listener(s) assert len(p0.get_event_listeners()) == 0 assert len(d1.get_event_listeners()) == 0 assert len(p1.get_event_listeners()) == 0
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) for i, c in enumerate([ X11Color("Red"), X11Color("Orange"), X11Color("Yellow"), X11Color("YellowGreen"), X11Color("Blue"), X11Color("Purple"), ]): ChunkOfText("Hello World!", font_size=Decimal(24), font_color=c).layout( page, Rectangle( Decimal(100 + i * 30), Decimal(724 - i * 30), Decimal(100), Decimal(100), ), ) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() bb = Rectangle(Decimal(20), Decimal(600), Decimal(500), Decimal(124)) for va in [Alignment.TOP, Alignment.MIDDLE, Alignment.BOTTOM]: for ha in [Alignment.LEFT, Alignment.CENTERED, Alignment.RIGHT]: page = Page() pdf.append_page(page) Paragraph( "%s %s - Once upon a midnight dreary, while I pondered weak and weary, over many a quaint and curious volume of forgotten lore" % (str(va), str(ha)), font_size=Decimal(20), vertical_alignment=va, horizontal_alignment=ha, padding_top=Decimal(5), padding_right=Decimal(5), padding_bottom=Decimal(5), padding_left=Decimal(5), background_color=X11Color("Salmon"), ).layout( page, bb, ) # add rectangle annotation page.append_square_annotation( stroke_color=X11Color("Red"), rectangle=bb, ) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = MultiColumnLayout(page, number_of_columns=2) layout.add( Paragraph( "The Raven", font_size=Decimal(20), font="Helvetica-Oblique", font_color=HexColor("708090"), )) layout.add( Paragraph( """Once upon a midnight dreary, while I pondered, weak and weary, Over many a quaint and curious volume of forgotten lore- While I nodded, nearly napping, suddenly there came a tapping, As of some one gently rapping, rapping at my chamber door. 'Tis some visitor,' I muttered, 'tapping at my chamber door- Only this and nothing more.'""", text_alignment=Alignment.CENTERED, font_size=Decimal(7), respect_newlines_in_text=True, )) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) padding = Decimal(5) layout_rect = Paragraph( "Once upon a midnight dreary,\nwhile I pondered weak and weary,\nover many a quaint and curious\nvolume of forgotten lore", font_size=Decimal(20), horizontal_alignment=Alignment.CENTERED, text_alignment=Alignment.CENTERED, respect_newlines_in_text=True, padding_top=padding, padding_right=padding, padding_bottom=padding, padding_left=padding, border_right=True, border_top=True, border_color=X11Color("Green"), font_color=X11Color("Salmon"), ).layout( page, Rectangle(Decimal(20), Decimal(600), Decimal(500), Decimal(124)), ) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) t = Table(number_of_rows=3, number_of_columns=2) # row 0 t.add(Paragraph("A")) t.add(Paragraph("B")) # row 1 t.add(Paragraph(" ", respect_spaces_in_text=True)) t.add(Paragraph(" ", respect_spaces_in_text=True)) # row 2 t.add(Paragraph(" ", respect_spaces_in_text=True)) t.add(Paragraph(" ", respect_spaces_in_text=True)) t.set_padding_on_all_cells(Decimal(5), Decimal(5), Decimal(5), Decimal(5)) layout.add(t) # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page(s) N = 3 for i in range(0, N): page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) layout.add(Paragraph("Page %d of %d" % (i + 1, N))) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) layout.add( Paragraph( "Once upon a midnight dreary, while I pondered weak and weary, over many a quaint and curious volume of forgotten lore.", font_size=Decimal(20), text_alignment=Alignment.RIGHT, horizontal_alignment=Alignment.RIGHT, )) layout.add( Paragraph( "While I nodded, nearly napping, suddenly there came a tapping. As of someone gently rapping, rapping at my chamberdoor.", font_size=Decimal(20), text_alignment=Alignment.RIGHT, horizontal_alignment=Alignment.RIGHT, )) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) t = Table(number_of_columns=10, number_of_rows=25) for _ in range(0, 10): for _ in range(0, 25): put_star = random.choice([x <= 3 for x in range(0, 10)]) if put_star: c: Color = random.choice(self.COLORS) s: Decimal = random.choice( [ Decimal(16), Decimal(16), Decimal(16), Decimal(16), Decimal(8), Decimal(4), ] ) t.add( Shape( LineArtFactory.n_pointed_star( bounding_box=Rectangle(Decimal(0), Decimal(0), s, s), n=random.choice([3, 5, 7, 12]), ), fill_color=c, stroke_color=c, line_width=Decimal(1), ) ) else: t.add(Paragraph(" ", respect_spaces_in_text=True)) t.no_borders() t.set_padding_on_all_cells(Decimal(5), Decimal(5), Decimal(5), Decimal(5)) layout.add(t) # footer rectangle_box = Rectangle( Decimal(0), Decimal(0), page.get_page_info().get_width(), page.get_page_info().get_height() * Decimal(0.1), ) Shape( LineArtFactory.rectangle(rectangle_box), fill_color=self.COLORS[0], stroke_color=self.COLORS[0], line_width=Decimal(1), ).layout(page, rectangle_box) rectangle_box = Rectangle( Decimal(0), page.get_page_info().get_height() * Decimal(0.1), page.get_page_info().get_width(), Decimal(2), ) Shape( LineArtFactory.rectangle(rectangle_box), fill_color=self.COLORS[1], stroke_color=self.COLORS[1], line_width=Decimal(1), ).layout(page, rectangle_box) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): sentences = [ "THE BOAT WILL ARRIVE ON MONDAY", "SHE LIVES AT THE HOUSE WITH THE BLUE DOOR", "A FRIEND IN NEED IS A FRIEND INDEED", "AN APPLE A DAY KEEPS THE DOCTOR AWAY", ] pdf = Document() page = Page() pdf.append_page(page) # layout layout = SingleColumnLayout(page) # add title layout.add( Paragraph( "Reverse the words", font_size=Decimal(20), font_color=X11Color("YellowGreen"), )) # add text layout.add( Paragraph( """ This is perhaps the simplest code to use and solve. Simply read each word backwards. """, font_color=X11Color("SlateGray"), font_size=Decimal(8), )) # add grid t = Table( number_of_rows=len(sentences) * 2, number_of_columns=2, column_widths=[Decimal(1), Decimal(9)], ) for i, s in enumerate(sentences): # code word coded_sentence = "".join([ "".join([y for y in reversed(x)]) + " " for x in s.split(" ") ]) t.add( TableCell( Paragraph(str(i + 1) + "."), border_top=False, border_right=False, border_left=False, border_bottom=False, row_span=2, )) t.add( TableCell( Paragraph(coded_sentence, respect_spaces_in_text=True), border_top=False, border_right=False, border_left=False, border_bottom=False, )) t.add( TableCell( Paragraph(".."), border_top=False, border_right=False, border_left=False, border_bottom=True, )) t.set_padding_on_all_cells(Decimal(15), Decimal(5), Decimal(5), Decimal(5)) layout.add(t) # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) # add title layout.add( Paragraph( "Match Up Puzzle", font_size=Decimal(20), font_color=X11Color("YellowGreen"), ) ) # add explanation layout.add( Paragraph( """ These simple "match up" puzzles help children with observation skills. They will also need to learn a way of marking or remembering which items they have matched, so that they can identify the odd ones out. If you would like to reuse puzzles you could place counters on each "pair" that your child finds, perhaps.""", font_color=X11Color("SlateGray"), font_size=Decimal(8), ) ) # random locations for each image imgs = [ "https://icons.iconarchive.com/icons/chanut/role-playing/128/Orc-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/King-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Knight-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Medusa-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Monster-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Sorceress-Witch-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Centaur-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Elf-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Poison-Spider-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Unicorn-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Viking-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Villager-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Dragon-Egg-icon.png", ] N = 10 random.shuffle(imgs) image_positions: typing.Dict[int, str] = {} for i, img_url in enumerate(imgs[0 : (N + 1)]): # place image 1 p0 = random.randint(0, N ** 2) while p0 in image_positions: p0 = random.randint(0, N ** 2) image_positions[p0] = img_url if i != 0: # place image 2 p1 = random.randint(0, N ** 2) while p1 in image_positions: p1 = random.randint(0, N ** 2) image_positions[p1] = img_url t = Table(number_of_rows=N, number_of_columns=N) for i in range(0, N ** 2): if i in image_positions: t.add(Image(image_positions[i], width=Decimal(32), height=Decimal(32))) else: t.add(Paragraph(" ", respect_spaces_in_text=True)) t.no_borders() t.set_padding_on_all_cells(Decimal(2), Decimal(2), Decimal(2), Decimal(2)) layout.add(t) # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = MultiColumnLayout(page) # background self._write_background(page) # table avatar_urls = [ "https://avatars.githubusercontent.com/u/" + str(x) for x in self.FIRST_100_STARS ] t = Table(number_of_columns=4, number_of_rows=25) for s in avatar_urls[0 : (4 * 25)]: im = PILImage.open(requests.get(s, stream=True).raw) t.add(Image(im, width=Decimal(20), height=Decimal(20))) t.set_padding_on_all_cells(Decimal(2), Decimal(2), Decimal(2), Decimal(2)) t.no_borders() layout.add(t) layout.add( Paragraph( "100 stars!", font="Helvetica-Bold", font_size=Decimal(20), font_color=self.ACCENT_COLOR_1, horizontal_alignment=Alignment.CENTERED, ) ) # next column layout.switch_to_next_column() # paragraph layout.add( Paragraph( "Thank you,", font="Helvetica-Bold", font_size=Decimal(20), font_color=self.ACCENT_COLOR_1, ) ) layout.add( Paragraph( "Your support and encouragement have always been the driving factors in the development of pText. " "I want you to know that I value your appreciation immensely!" ) ) layout.add( Paragraph( "-- Joris Schellekens", font="Helvetica-Oblique", font_size=Decimal(8), font_color=self.ACCENT_COLOR_2, ) ) layout.add( Barcode( data="https://github.com/jorisschellekens/ptext-release/stargazers", type=BarcodeType.QR, width=Decimal(128), stroke_color=self.ACCENT_COLOR_1, ) ) # footer rectangle_box = Rectangle( Decimal(0), Decimal(0), page.get_page_info().get_width(), page.get_page_info().get_height() * Decimal(0.1), ) Shape( LineArtFactory.rectangle(rectangle_box), fill_color=self.ACCENT_COLOR_1, stroke_color=self.ACCENT_COLOR_1, line_width=Decimal(1), ).layout(page, rectangle_box) rectangle_box = Rectangle( Decimal(0), page.get_page_info().get_height() * Decimal(0.1), page.get_page_info().get_width(), Decimal(2), ) Shape( LineArtFactory.rectangle(rectangle_box), fill_color=self.ACCENT_COLOR_2, stroke_color=self.ACCENT_COLOR_2, line_width=Decimal(1), ).layout(page, rectangle_box) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) t = Table(number_of_rows=10, number_of_columns=4) t.add( Paragraph( "lowercase", font_color=X11Color("YellowGreen"), horizontal_alignment=Alignment.CENTERED, )) t.add( Paragraph( "uppercase", font_color=X11Color("YellowGreen"), horizontal_alignment=Alignment.CENTERED, )) t.add( Paragraph( "lowercase acute", font_color=X11Color("YellowGreen"), horizontal_alignment=Alignment.CENTERED, )) t.add( Paragraph( "uppercase acute", font_color=X11Color("YellowGreen"), horizontal_alignment=Alignment.CENTERED, )) # A font: Font = TrueTypeFont.true_type_font_from_file( Path(__file__).parent / "Pacifico.ttf") t.add(Paragraph("a", font=font)) t.add(Paragraph("A", font=font)) t.add(Paragraph("á", font=font)) t.add(Paragraph("Á", font=font)) # B t.add(Paragraph("b", font=font)) t.add(Paragraph("B", font=font)) t.add(Paragraph("-", font=font)) t.add(Paragraph("-", font=font)) # C t.add(Paragraph("c", font=font)) t.add(Paragraph("C", font=font)) t.add(Paragraph("-", font=font)) t.add(Paragraph("-", font=font)) # D t.add(Paragraph("d", font=font)) t.add(Paragraph("D", font=font)) t.add(Paragraph("-", font=font)) t.add(Paragraph("-", font=font)) # E t.add(Paragraph("e", font=font)) t.add(Paragraph("E", font=font)) t.add(Paragraph("é", font=font)) t.add(Paragraph("É", font=font)) # F t.add(Paragraph("f", font=font)) t.add(Paragraph("F", font=font)) t.add(Paragraph("-", font=font)) t.add(Paragraph("-", font=font)) # G t.add(Paragraph("g", font=font)) t.add(Paragraph("G", font=font)) t.add(Paragraph("-", font=font)) t.add(Paragraph("-", font=font)) # .. t.add( Paragraph( "...", font_color=X11Color("LightGray"), horizontal_alignment=Alignment.CENTERED, )) t.add( Paragraph( "...", font_color=X11Color("LightGray"), horizontal_alignment=Alignment.CENTERED, )) t.add( Paragraph( "...", font_color=X11Color("LightGray"), horizontal_alignment=Alignment.CENTERED, )) t.add( Paragraph( "...", font_color=X11Color("LightGray"), horizontal_alignment=Alignment.CENTERED, )) # Z t.add(Paragraph("z", font=font)) t.add(Paragraph("Z", font=font)) t.add(Paragraph("-", font=font)) t.add(Paragraph("-", font=font)) t.set_border_width_on_all_cells(Decimal(0.2)) t.set_padding_on_all_cells(Decimal(5), Decimal(5), Decimal(5), Decimal(5)) layout.add(t) layout.add( Paragraph( text= "**These are the characters pText can currently render in a PDF", font_size=Decimal(8), font_color=X11Color("Gray"), horizontal_alignment=Alignment.RIGHT, )) # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # fmt: off mazes = [ ("https://i.pinimg.com/originals/1e/c2/a7/1ec2a73d0a45016c7d1b52ef9f11e740.png", "395E66"), ("https://i.pinimg.com/originals/f8/23/88/f823882e7c5fa42790e78f43ecf7e8bf.jpg", "387D7A"), ("https://i.pinimg.com/600x315/2d/94/33/2d94334b737efb5d3a5ef32aef9daefc.jpg", "32936F"), ("https://i.pinimg.com/originals/f1/c9/07/f1c907c09d65d5c86fba304fed1009ca.jpg", "26A96C"), ("https://cdn.pixabay.com/photo/2017/08/24/12/11/silhouette-2676573_960_720.png", "2BC016"), ("https://images-na.ssl-images-amazon.com/images/I/61bqYbAeUgL._AC_SL1500_.jpg", "395E66"), ("https://i.pinimg.com/originals/55/e8/91/55e891af7de086a8868e1a8e02fb4426.jpg","387D7A"), ("https://cdn.shopify.com/s/files/1/2123/8425/products/166422700-LRG_242a4c8b-cad5-476e-afd1-c8b882d48fc2_530x.jpg","32936F"), ("http://www.silhcdn.com/3/i/shapes/lg/7/6/d124067.jpg","26A96C"), ("https://cdn.pixabay.com/photo/2018/03/04/23/28/frog-3199601_1280.png","2BC016") ] # fmt: on # add mazes for (url, color) in mazes: for _ in range(0, 3): self._write_maze_page(pdf, url, color) # add ack page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) # content of ack page layout.add( Paragraph( "Hi there,", font_color=HexColor("32936F"), font_size=Decimal(20), ) ) layout.add( Paragraph( "This PDF was made by pText. Check out the GitHub repository to find more fun examples of what you can do with PDF's.", font_color=X11Color("SlateGray"), font_size=Decimal(12), ) ) layout.add( Barcode( data="https://github.com/jorisschellekens/ptext-release", type=BarcodeType.QR, width=Decimal(64), ) ) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)