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) 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 empty document pdf: Document = Document() # create empty page page: Page = Page() # add page to document pdf.append_page(page) # add Image layout = SingleColumnLayout(page) # add image im = PILImage.open( requests.get( "https://images.unsplash.com/photo-1597826368522-9f4cb5a6ba48?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw", stream=True, ).raw ) layout.add(Image(im, width=Decimal(256))) # 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) # set layout layout = SingleColumnLayout(page) # add barcode layout.add( Barcode( data="123456789128", type=BarcodeType.CODE_128, width=Decimal(128), stroke_color=HexColor("#080708"), )) # 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): 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) 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) # 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): # 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 _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 _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 = 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 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) 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 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 = 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) # first layer, displaying a raven layout = SingleColumnLayout(page) for _ in range(0, 12): layout.add(Paragraph(" ", respect_spaces_in_text=True)) layout.add( Image( "https://cdn3.vectorstock.com/i/1000x1000/03/47/black-raven-on-white-background-vector-4780347.jpg" )) # second layer, displaying the poem 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.'""", horizontal_alignment=Alignment.CENTERED, font_size=Decimal(7), respect_newlines_in_text=True, )) layout.add( Paragraph( """Ah, distinctly I remember it was in the bleak December; And each separate dying ember wrought its ghost upon the floor. Eagerly I wished the morrow;-vainly I had sought to borrow From my books surcease of sorrow-sorrow for the lost Lenore- For the rare and radiant maiden whom the angels name Lenore- Nameless here for evermore.""", horizontal_alignment=Alignment.CENTERED, font_size=Decimal(7), respect_newlines_in_text=True, )) layout.add( Paragraph( """And the silken, sad, uncertain rustling of each purple curtain Thrilled me-filled me with fantastic terrors never felt before; So that now, to still the beating of my heart, I stood repeating 'Tis some visitor entreating entrance at my chamber door- Some late visitor entreating entrance at my chamber door;- This it is and nothing more.'""", horizontal_alignment=Alignment.CENTERED, font_size=Decimal(7), respect_newlines_in_text=True, )) layout.add( Paragraph( """Presently my soul grew stronger; hesitating then no longer, 'Sir,' said I, 'or Madam, truly your forgiveness I implore; But the fact is I was napping, and so gently you came rapping, And so faintly you came tapping, tapping at my chamber door, That I scarce was sure I heard you'-here I opened wide the door;- Darkness there and nothing more.""", horizontal_alignment=Alignment.CENTERED, font_size=Decimal(7), respect_newlines_in_text=True, )) layout.switch_to_next_column() layout.add( Paragraph( """Deep into that darkness peering, long I stood there wondering, fearing, Doubting, dreaming dreams no mortal ever dared to dream before; But the silence was unbroken, and the stillness gave no token, And the only word there spoken was the whispered word, 'Lenore?' This I whispered, and an echo murmured back the word, 'Lenore!'- Merely this and nothing more.""", horizontal_alignment=Alignment.CENTERED, font_size=Decimal(7), respect_newlines_in_text=True, )) layout.add( Paragraph( """Back into the chamber turning, all my soul within me burning, Soon again I heard a tapping somewhat louder than before. 'Surely,' said I, 'surely that is something at my window lattice; Let me see, then, what thereat is, and this mystery explore- Let my heart be still a moment and this mystery explore;- 'Tis the wind and nothing more!'""", horizontal_alignment=Alignment.CENTERED, font_size=Decimal(7), respect_newlines_in_text=True, )) layout.add( Paragraph( """Open here I flung the shutter, when, with many a flirt and flutter, In there stepped a stately Raven of the saintly days of yore; Not the least obeisance made he; not a minute stopped or stayed he; But, with mien of lord or lady, perched above my chamber door- Perched upon a bust of Pallas just above my chamber door- Perched, and sat, and nothing more.""", horizontal_alignment=Alignment.CENTERED, font_size=Decimal(7), respect_newlines_in_text=True, )) layout.add( Paragraph( """Then this ebony bird beguiling my sad fancy into smiling, By the grave and stern decorum of the countenance it wore, 'Though thy crest be shorn and shaven, thou,' I said, 'art sure no craven, Ghastly grim and ancient Raven wandering from the Nightly shore- Tell me what thy lordly name is on the Night's Plutonian shore!' Quoth the Raven 'Nevermore.'""", horizontal_alignment=Alignment.CENTERED, font_size=Decimal(7), respect_newlines_in_text=True, )) layout.add( Paragraph( """Much I marvelled this ungainly fowl to hear discourse so plainly, Though its answer little meaning-little relevancy bore; For we cannot help agreeing that no living human being Ever yet was blessed with seeing bird above his chamber door- Bird or beast upon the sculptured bust above his chamber door, With such name as 'Nevermore.'""", horizontal_alignment=Alignment.CENTERED, font_size=Decimal(7), respect_newlines_in_text=True, )) layout.add( Paragraph( """But the Raven, sitting lonely on the placid bust, spoke only That one word, as if his soul in that one word he did outpour. Nothing farther then he uttered-not a feather then he fluttered- Till I scarcely more than muttered 'Other friends have flown before- On the morrow he will leave me, as my Hopes have flown before.' Then the bird said 'Nevermore.'""", horizontal_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) 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 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 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), ) ) # 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 = TestWriteGridCopyPuzzle._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, 32): for j in range(0, 256): pixels_a[(i, j)] = (0, 0, 0) pixels_b[(i, j)] = (0, 0, 0) pixels_a[(j, i)] = (0, 0, 0) pixels_b[(j, i)] = (0, 0, 0) for j in range(0, 256): pixels_a[(255, j)] = (0, 0, 0) pixels_b[(255, j)] = (0, 0, 0) pixels_a[(j, 255)] = (0, 0, 0) pixels_b[(j, 255)] = (0, 0, 0) 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) # path to font font_path: Path = Path(__file__).parent / "Jsfont-Regular.ttf" assert font_path.exists() # layout layout: PageLayout = SingleColumnLayout(page) layout.add( Paragraph( "Hello World,", font=TrueTypeFont.true_type_font_from_file(font_path), font_size=Decimal(40), font_color=HexColor("FFCC00"), )) layout.add( Paragraph( """ pText can now write in any custom TTF font. Like this one, which is a stylized copy of my handwriting. Of course, you could also pick a more sensible font. Like Comic Sans, Roboto, Open Sans, and many others. In fact, pText can use any TTF font. """, font=TrueTypeFont.true_type_font_from_file(font_path), font_size=Decimal(20), font_color=HexColor("646E78"), )) layout.add( Paragraph( """ Finally, you have the option to match your own custom style and brand. For more examples, check out GitHub. Don't forget to star the repo :-) """, font=TrueTypeFont.true_type_font_from_file(font_path), font_size=Decimal(20), font_color=HexColor("68C3D4"), )) layout.add( Barcode( data="https://github.com/jorisschellekens/ptext-release", type=BarcodeType.QR, width=Decimal(64), height=Decimal(64), stroke_color=HexColor("FFCC00"), )) # 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) # 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): # 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) 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) page_layout: PageLayout = SingleColumnLayout(page) page_layout.vertical_margin = page.get_page_info().get_height( ) * Decimal(0.02) page_layout.add( Image( "https://www.designevo.com/res/templates/thumb_small/green-and-white-stripe-sphere.png", width=Decimal(128), height=Decimal(128), )) # invoice information table page_layout.add(self._build_invoice_information()) # empty paragraph for spacing page_layout.add(Paragraph(" ")) # billing and shipping information page_layout.add(self._build_billing_and_shipping_information()) # empty paragraph for spacing page_layout.add(Paragraph(" ")) # itemized description page_layout.add(self._build_itemized_description_table()) # outline pdf.add_outline("Your Invoice", 0, DestinationType.FIT, page_nr=0) # embed json document for machine processing invoice_json = { "items": [ { "Description": "Product1", "Quantity": 2, "Unit Price": 50, "Amount": 100, }, { "Description": "Product2", "Quantity": 4, "Unit Price": 60, "Amount": 100, }, { "Description": "Labor", "Quantity": 14, "Unit Price": 60, "Amount": 100, }, ], "Subtotal": 1180, "Discounts": 177, "Taxes": 100.30, "Total": 1163.30, } invoice_json_bytes = bytes(json.dumps(invoice_json, indent=4), encoding="latin1") pdf.append_embedded_file("invoice.json", invoice_json_bytes, apply_compression=False) # 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() 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() # 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)