def _build_table_for_sentence(self, sentence: str) -> Table: t = Table(number_of_columns=len(sentence), number_of_rows=3) for c in sentence: if c in [".", "?", "!", ",", " "]: t.add( TableCell( Paragraph(c, respect_spaces_in_text=True), background_color=X11Color("SlateGray"), ) ) else: num = ord(c.upper()) - ord("A") + 1 t.add( Paragraph( str(num), font_size=Decimal(6), text_alignment=Alignment.CENTERED, ) ) for c in sentence: t.add(Paragraph(" ", respect_spaces_in_text=True)) for c in sentence: t.add( TableCell( Paragraph(" ", respect_spaces_in_text=True), border_top=False, border_left=False, border_right=False, ) ) t.set_padding_on_all_cells(Decimal(2), Decimal(2), Decimal(2), Decimal(2)) return t
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): 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 empty document pdf: Document = Document() # create empty page page: Page = Page() # add page to document pdf.append_page(page) # set layout layout = MultiColumnLayout(page) # add chart layout.add(Chart(self._create_plot())) layout.switch_to_next_column() # add Heading layout.add( Heading( "3D Density Chart", font_color=X11Color("YellowGreen"), font_size=Decimal(20), ) ) layout.add( Paragraph( "The mplot3D toolkit of Matplotlib allows to easily create 3D scatterplots. " "Note that most of the customisations presented in the Scatterplot section will work in 3D as well. " "The result can be a bit disappointing since each marker is represented as a dot, not as a sphere.." ) ) layout.add( Paragraph( "Check out https://python-graph-gallery.com/ for more wonderful examples of plots in Python." ) ) layout.add( Barcode( data="https://python-graph-gallery.com/", type=BarcodeType.QR, stroke_color=X11Color("YellowGreen"), ) ) # write file = self.output_dir / "output.pdf" with open(file, "wb") as pdf_file_handle: PDF.dumps(pdf_file_handle, pdf) 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) # 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 empty document pdf: Document = Document() # create empty page page: Page = Page() # add page to document pdf.append_page(page) # set layout layout = MultiColumnLayout(page) # add chart layout.add(Chart(self._create_plot())) layout.switch_to_next_column() # add Heading layout.add( Heading("3D Plot", font_color=X11Color("Orange"), font_size=Decimal(20))) layout.add( Paragraph( "Demonstrates plotting a 3D surface colored with the coolwarm color map. " "The surface can be made opaque by using antialiased=False.")) layout.add( Paragraph( "Check out https://python-graph-gallery.com/ for more wonderful examples of plots in Python." )) layout.add( Barcode( data="https://python-graph-gallery.com/", type=BarcodeType.QR, stroke_color=X11Color("Orange"), )) # write file = self.output_dir / "output.pdf" with open(file, "wb") as pdf_file_handle: PDF.dumps(pdf_file_handle, pdf) 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 __init__(self, words: typing.List[str]): # set solved grid to None self.solved_grid: typing.Optional[typing.List[typing.List[str]]] = None # call recursive algo to find grid w = max([len(x) for x in words]) while self.solved_grid is None: # build empty grid g = [["." for _ in range(0, w)] for _ in range(0, w)] # recursion words = [x.upper() for x in words] random.shuffle(words) self._fit_words_in_grid(g, words) if self.solved_grid is None: w += 1 # call constructor super(WordSearch, self).__init__(number_of_rows=w, number_of_columns=w) # add elements for i in range(0, len(self.solved_grid)): for j in range(0, len(self.solved_grid[i])): self.add( Paragraph( text=self.solved_grid[i][j], horizontal_alignment=Alignment.CENTERED, )) # no border self.no_borders()
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 _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) # 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 = 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_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 = 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 _write_background(self, page: Page): layout = SingleColumnLayout(page) t = Table(number_of_columns=10, number_of_rows=25) for i in range(0, 25): for j in range(0, 10): put_star = random.choice([x <= 3 for x in range(0, 10)]) if i < 11 and j >= 5: t.add(Paragraph(" ", respect_spaces_in_text=True)) continue if put_star: c = random.choice( [ self.ACCENT_COLOR_1, self.ACCENT_COLOR_2, self.ACCENT_COLOR_3, self.ACCENT_COLOR_4, self.ACCENT_COLOR_5, ] ) t.add( Shape( LineArtFactory.n_pointed_star( bounding_box=Rectangle( Decimal(0), Decimal(0), Decimal(16), Decimal(16) ), 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)
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) 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() 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) 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(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) 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 empty document pdf: Document = Document() # create empty page page: Page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) # add title layout.add( Paragraph( "Complete the picture", font_size=Decimal(20), font_color=X11Color("YellowGreen"), )) layout.add( Paragraph( """ Can you complete the picture on the right by copying the completed picture on the left? """, respect_newlines_in_text=True, font_color=X11Color("SlateGray"), font_size=Decimal(8), )) # add image image_a = PILImage.open( requests.get( "https://www.mozilla.org/media/protocol/img/logos/firefox/browser/logo-lg-high-res.fbc7ffbb50fd.png", stream=True, ).raw) image_a = TestWriteCompleteThePictureHorizontallyPuzzle._convert_png_to_jpg( image_a) image_a = image_a.resize((256, 256)) image_b = PILImage.new(size=(256, 256), color=(255, 255, 255), mode="RGB") pixels_a = image_a.load() pixels_b = image_b.load() for i in range(0, 256): for j in range(0, 256): if i == 0 or j == 0 or i == 255 or j == 255 or i % 64 == 0: pixels_b[(i, j)] = (0, 0, 0) continue if int(i / 64) % 2 == 0: pixels_b[(i, j)] = pixels_a[(i, j)] t: Table = Table(number_of_columns=2, number_of_rows=1) t.add(Image(image_a)) t.add(Image(image_b)) t.no_borders() t.set_padding_on_all_cells(Decimal(5), Decimal(5), Decimal(5), Decimal(5)) layout.add(t) # write file = self.output_dir / "output.pdf" with open(file, "wb") as pdf_file_handle: PDF.dumps(pdf_file_handle, pdf) 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) 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 = 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 empty document pdf: Document = Document() # create empty page page: Page = Page() # add page to document pdf.append_page(page) # set layout layout = SingleColumnLayout(page) # add barcode layout.add( Table(number_of_rows=5, number_of_columns=2).add(Paragraph("CODE 128")).add( Barcode( data="123456789128", type=BarcodeType.CODE_128, width=Decimal(128), stroke_color=HexColor("#080708"), )).add(Paragraph("CODE 39")).add( Barcode( data="123456789128", type=BarcodeType.CODE_39, width=Decimal(128), stroke_color=HexColor("#3772FF"), )).add(Paragraph("EAN 13")).add( Barcode( data="123456789128", type=BarcodeType.EAN_13, width=Decimal(128), stroke_color=HexColor("#DF2935"), )).add(Paragraph("EAN 14")).add( Barcode( data="1234567891280", type=BarcodeType.EAN_14, width=Decimal(128), stroke_color=HexColor("#FDCA40"), )).add(Paragraph("QR")).add( Barcode( data="1234567891280", type=BarcodeType.QR, width=Decimal(128), stroke_color=HexColor("#E6E8E6"), fill_color=HexColor("#DF2935"), )).set_padding_on_all_cells( Decimal(10), Decimal(5), Decimal(5), Decimal(5))) # write file = self.output_dir / "output.pdf" with open(file, "wb") as pdf_file_handle: PDF.dumps(pdf_file_handle, pdf) 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 empty document pdf: Document = Document() # create empty page page: Page = Page() # add page to document pdf.append_page(page) # add Image layout = MultiColumnLayout(page) # add image layout.add( Image( "https://images.unsplash.com/photo-1550155864-3033f844da36?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80", width=Decimal(256), ) ) layout.switch_to_next_column() # add title layout.add( Paragraph( "Love you more", font_color=X11Color("Crimson"), font="Helvetica-Bold", font_size=Decimal(20), ) ) layout.add( Paragraph( """When I say I love you more, I don't just mean I love you more than you love me. I mean I love you more than the bad days ahead of us. I love you more than any fight we will ever have. I love you more than the distance between us. I love you more than any obstacle that could ever try and come between us. I love you the most. """, respect_newlines_in_text=True, ) ) layout.add( Paragraph( """yours, most sincerely JS """, font_color=X11Color("SlateGray"), font="Helvetica-Bold", font_size=Decimal(8), respect_newlines_in_text=True, ) ) # write file = self.output_dir / "output.pdf" with open(file, "wb") as pdf_file_handle: PDF.dumps(pdf_file_handle, pdf) 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) # 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)