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 _add_image_to_table(self, url: str, table: Table): im = PILImage.open(requests.get( url, stream=True, ).raw) table.add( Image( im, width=Decimal(128), height=Decimal(128), horizontal_alignment=Alignment.CENTERED, ))
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) 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 = 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 _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 empty document pdf: Document = Document() # create empty page page: Page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) # add title layout.add( Paragraph( "Copy the picture", font_size=Decimal(20), font_color=X11Color("YellowGreen"), )) layout.add( Paragraph( """ Copy the picture using the grid lines as a guide. You might find it easier to copy one square at a time. Count the squares carefully! """, respect_newlines_in_text=True, font_color=X11Color("SlateGray"), font_size=Decimal(8), )) pics: typing.List[Image] = self._split_picture_to_grid( "https://icons.iconarchive.com/icons/iconka/meow-2/128/cat-sing-icon.png" ) # fmt: off coords: typing.List[str] = [ "1A", "2A", "3A", "4A", "1B", "2B", "3B", "4B", "1C", "2C", "3C", "4C", "1D", "2D", "3D", "4D", ] # fmt: on pics_and_coords = [x for x in zip(pics, coords)] random.shuffle(pics_and_coords) t = Table( number_of_rows=4, number_of_columns=8, column_widths=[ Decimal(1), Decimal(3), Decimal(1), Decimal(3), Decimal(1), Decimal(3), Decimal(1), Decimal(3), ], ) for p, c in pics_and_coords: t.add(Paragraph(c, font_size=Decimal(6))) t.add(Image(p)) t.set_padding_on_all_cells(Decimal(2), Decimal(2), Decimal(2), Decimal(2)) t.no_borders() layout.add(t) # table 2 t2 = Table(number_of_columns=4, number_of_rows=4) for c in coords: t2.add( Paragraph( "\n" + c + "\n", respect_newlines_in_text=True, font_color=X11Color("LightGray"), horizontal_alignment=Alignment.CENTERED, )) t2.set_padding_on_all_cells(Decimal(2), Decimal(2), Decimal(2), Decimal(2)) layout.add(t2) # 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 = 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() # add page page = Page() pdf.append_page(page) t = Table(number_of_rows=5, number_of_columns=3) t.add(Paragraph(" ", respect_spaces_in_text=True)) t.add( Paragraph( "Close-up", font_color=X11Color("SteelBlue"), font_size=Decimal(20), horizontal_alignment=Alignment.CENTERED, )) t.add( Paragraph( "Panoramic", font_color=X11Color("SteelBlue"), font_size=Decimal(20), horizontal_alignment=Alignment.CENTERED, )) t.add(Paragraph("Nature")) self._add_image_to_table( "https://images.unsplash.com/photo-1520860560195-0f14c411476e?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw", t, ) self._add_image_to_table( "https://images.unsplash.com/photo-1613480123595-c5582aa551b9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw", t, ) t.add(Paragraph("Architecture")) self._add_image_to_table( "https://images.unsplash.com/photo-1611321569296-1305a38ebd74?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw", t, ) self._add_image_to_table( "https://images.unsplash.com/photo-1613262666714-acebcc37f11e?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw", t, ) 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 = SingleColumnLayout(page) 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) t = Table(number_of_rows=5, number_of_columns=3) t.add( TableCell( Paragraph(" ", respect_spaces_in_text=True), border_top=False, border_left=False, ) ) t.add( Paragraph( "Language", font_color=X11Color("SteelBlue"), font_size=Decimal(20) ) ) t.add( Paragraph( "Nof. Questions", font_color=X11Color("SteelBlue"), font_size=Decimal(20), ) ) t.add( TableCell( Paragraph("front-end", font_color=X11Color("SteelBlue")), row_span=2 ) ) t.add(Paragraph("Javascript")) t.add(Paragraph("2,167,178")) t.add(Paragraph("Php")) t.add(Paragraph("1,391,524")) t.add( TableCell( Paragraph("back-end", font_color=X11Color("SteelBlue")), row_span=2 ) ) t.add(Paragraph("C++")) t.add(Paragraph("711,944")) t.add(Paragraph("Java")) t.add(Paragraph("1,752,877")) t.set_border_width_on_all_cells(Decimal(0.2)) t.set_padding_on_all_cells(Decimal(5), Decimal(5), Decimal(5), Decimal(5)) table_rect = t.layout( page, bounding_box=Rectangle( Decimal(20), Decimal(600), Decimal(500), Decimal(200) ), ) Paragraph( text="**Data gathered from Stackoverflow.com on 10th of february 2021", font_size=Decimal(8), font_color=X11Color("Gray"), ).layout( page, bounding_box=Rectangle( Decimal(20), table_rect.y - 40, table_rect.width, Decimal(20) ), ) # 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() page: Page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) # add title layout.add( Paragraph( "Match The Shadow", 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), )) # add 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 imgs = imgs[0:N] random.shuffle(imgs) shadows = [ TestMatchTheShadowPuzzle._make_image_shadow(x) for x in imgs ] random.shuffle(imgs) t = Table(number_of_columns=5, number_of_rows=N) for i in range(0, N): t.add(Image(imgs[i], width=Decimal(32), height=Decimal(32))) t.add(Paragraph(" ", respect_spaces_in_text=True)) t.add(Paragraph(" ", respect_spaces_in_text=True)) t.add(Paragraph(" ", respect_spaces_in_text=True)) t.add(Image(shadows[i], width=Decimal(32), height=Decimal(32))) 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) # set layout layout = SingleColumnLayout(page) my_dict = { " ": ["A Error", "B Error", "C Error", "D Error"], "lab1": [0.34, 0.23, 0.80, 0.79], "lab2": [0.53, 0.38, 0.96, 1.25], "lab3": [0.40, 0.27, 0.68, 0.93], } colors = { 0: X11Color("Green"), 0.25: X11Color("Yellow"), 0.5: X11Color("Orange"), 0.75: X11Color("Red"), } table = Table(number_of_rows=4, number_of_columns=5) table.add(Paragraph(" ", respect_spaces_in_text=True)) for h in my_dict[" "]: table.add( Paragraph(text=h, font="Helvetica-Bold", font_size=Decimal(12))) for name, row in [(k, v) for k, v in my_dict.items() if k != " "]: table.add(Paragraph(name)) for v in row: c = X11Color("Green") for b, bc in colors.items(): if v > b: c = bc table.add( Paragraph(str(v), font_color=c, horizontal_alignment=Alignment.CENTERED)) # set border table.set_border_width_on_all_cells(Decimal(0.2)) # set padding table.set_padding_on_all_cells(Decimal(5), Decimal(5), Decimal(5), Decimal(5)) # add to layout layout.add( Paragraph( "This table contains all measurands for 3 lab-sessions:")) layout.add(table) # 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): words = [ "COW", "PUPPY", "TURTLE", "PARROT", "SNAKE", "GOLDFISH", "HAMSTER", "KITTEN", "TURKEY", "DOVE", "HORSE", "BEE" "RABBIT", "DUCK", "SHRIMP", "PIG", "GOAT", "CRAB", "DOG", "DEER", "CAT", "MOUSE", "ELEPHANT", "LION", "PENGUIN", "SPARROW", "STORK", "HAWK", ] pdf = Document() page = Page() pdf.append_page(page) # layout layout = MultiColumnLayout(page, number_of_columns=2) # add title layout.add( Paragraph( """ Word scrambles or anagrams are an excellent way of helping children with their spelling as they have to recognise letter patterns. They are also a fun way of testing knowledge on a subject. We have word scrambles on lots of topics ready to print for home or classroom. """, font_color=X11Color("SlateGray"), font_size=Decimal(8), )) # add table t = Table(number_of_rows=len(words), number_of_columns=2) for i, w in enumerate(words): # shuffle word permuted_word = w while permuted_word == w: letters = [x for x in w] random.shuffle(letters) permuted_word = "".join([x for x in letters]) t.add( TableCell( Paragraph(str(i + 1) + ". " + permuted_word), border_top=False, border_right=False, border_left=False, border_bottom=False, )) # empty column t.add( TableCell( Paragraph(""), border_top=False, border_right=False, border_left=False, )) layout.add(t) # go to next column layout.switch_to_next_column() # add title layout.add( Paragraph( "Word Scramble", font_size=Decimal(20), font_color=X11Color("YellowGreen"), )) # add Image layout.add( Image( "https://www.how-to-draw-funny-cartoons.com/images/cartoon-tree-012.jpg" )) # 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("Flowchart Line Art", font_size=Decimal(20), font_color=X11Color("Blue"))) # table fixed_bb = Rectangle(Decimal(0), Decimal(0), Decimal(100), Decimal(100)) t = Table(number_of_rows=10, number_of_columns=6) t.add( Shape( LineArtFactory.flowchart_process(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_decision(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_document(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_predefined_document(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.regular_n_gon(fixed_bb, 8), # LineArtFactory.flowchart_multiple_documents(fixed_bb) fill_color=X11Color("White"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_data(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) # captions t.add(Paragraph("Process")) t.add(Paragraph("Decision")) t.add(Paragraph("Document")) t.add(Paragraph("Predefined Document")) t.add(Paragraph("Multiple Documents")) t.add(Paragraph("Data")) # second row of shapes t.add( Shape( LineArtFactory.flowchart_predefined_process(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.regular_n_gon(fixed_bb, 8), # LineArtFactory.flowchart_stored_data(fixed_bb), fill_color=X11Color("White"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_internal_storage(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_sequential_data(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.regular_n_gon(fixed_bb, 8), # LineArtFactory.flowchart_direct_data(fixed_bb), fill_color=X11Color("White"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_manual_input(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) # captions t.add(Paragraph("Predefined Process")) t.add(Paragraph("Stored Data")) t.add(Paragraph("Internal Storage")) t.add(Paragraph("Sequential Data")) t.add(Paragraph("Direct Data")) t.add(Paragraph("Manual Input")) # third row of shapes t.add( Shape( LineArtFactory.flowchart_manual_operation(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_card(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_paper_tape(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.regular_n_gon(fixed_bb, 8), # LineArtFactory.flowchart_display(fixed_bb), fill_color=X11Color("White"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_preparation(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_loop_limit(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) # captions t.add(Paragraph("Manual Operation")) t.add(Paragraph("Card")) t.add(Paragraph("Paper Tape")) t.add(Paragraph("Display")) t.add(Paragraph("Preparation")) t.add(Paragraph("Loop Limit")) # fourth row of shapes t.add( Shape( # LineArtFactory.flowchart_termination(fixed_bb), LineArtFactory.regular_n_gon(fixed_bb, 8), fill_color=X11Color("White"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_collate(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.regular_n_gon(fixed_bb, 8), # LineArtFactory.flowchart_delay(fixed_bb), fill_color=X11Color("White"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_extract(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_merge(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_or(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) # captions t.add(Paragraph("Termination")) t.add(Paragraph("Collate")) t.add(Paragraph("Delay")) t.add(Paragraph("Extract")) t.add(Paragraph("Merge")) t.add(Paragraph("Or")) # fifth row of shapes t.add( Shape( LineArtFactory.flowchart_sort(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_summing_junction(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.regular_n_gon(fixed_bb, 8), # LineArtFactory.flowchart_database(fixed_bb), fill_color=X11Color("White"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_on_page_reference(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_off_page_reference(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) t.add( Shape( LineArtFactory.flowchart_process_iso_9000(fixed_bb), fill_color=X11Color("Blue"), stroke_color=X11Color("Black"), line_width=Decimal(1), )) # captions t.add(Paragraph("Sort")) t.add(Paragraph("Summing Junction")) t.add(Paragraph("Database")) t.add(Paragraph("On Page Reference")) t.add(Paragraph("Off Page Reference")) t.add(Paragraph("Process ISO 9000")) 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) t = Table(number_of_rows=20, number_of_columns=20) colors = [ HSVColor(Decimal(x / 360), Decimal(1), Decimal(1)) for x in range(0, 360, int(360 / 20)) ] for i in range(0, 18 * 20): t.add( TableCell( Paragraph(" ", respect_spaces_in_text=True), background_color=colors[i % len(colors)], )) for i in range(0, 20): t.add( TableCell( Paragraph(" ", respect_spaces_in_text=True), background_color=colors[i % len(colors)].darker(), )) for i in range(0, 20): t.add( TableCell( Paragraph(" ", respect_spaces_in_text=True), background_color=colors[i % len(colors)].darker().darker(), )) t.no_borders() t.set_padding_on_all_cells(Decimal(5), Decimal(5), Decimal(5), Decimal(5)) table_rect = t.layout( page, bounding_box=Rectangle(Decimal(20), Decimal(600), Decimal(500), Decimal(200)), ) Paragraph( text="Love is love", font_size=Decimal(8), font_color=X11Color("Gray"), horizontal_alignment=Alignment.RIGHT, ).layout( page, bounding_box=Rectangle(Decimal(20), table_rect.y - 40, table_rect.width, Decimal(20)), ) # 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)