def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) for i in range(0, 10): space = "".join([" " for _ in range(0, i + 1)]) txt = "".join([ x + space for x in ["apple", "banana", "carrot", "dragonfruit"] ]) layout.add(Paragraph(txt, respect_spaces_in_text=True)) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) ChunkOfText( "é", font_size=Decimal(24), ).layout( page, Rectangle(Decimal(100), Decimal(600), Decimal(100), Decimal(100))) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_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_document(self, file): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / (file.stem + "_out.pdf") # attempt to read PDF doc_a = None with open(file, "rb") as in_file_handle: print("\treading (1) ..") doc_a = PDF.loads(in_file_handle) # attempt to read PDF with open(self.input_file_b, "rb") as in_file_handle_b: print("\treading (2) ..") doc_b = PDF.loads(in_file_handle_b) # concat all pages to same document doc_c = Document() doc_c.append_document(doc_a) doc_c.append_document(doc_b) # attempt to store PDF with open(out_file, "wb") as out_file_handle: print("\twrite ..") PDF.dumps(out_file_handle, doc_c) return True
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) Paragraph( "Once upon a midnight dreary, while I pondered weak and weary, over many a quaint and curious volume of forgotten lore", font_size=Decimal(20), horizontal_alignment=Alignment.RIGHT, ).layout( page, Rectangle(Decimal(20), Decimal(600), Decimal(500), Decimal(124)), ) # add rectangle annotation page.append_square_annotation( stroke_color=X11Color("Red"), rectangle=Rectangle(Decimal(20), Decimal(600), Decimal(500), Decimal(124)), ) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): sentences = [ "THE BOAT WILL ARRIVE ON MONDAY", "SHE LIVES AT THE HOUSE WITH THE BLUE DOOR", "A FRIEND IN NEED IS A FRIEND INDEED", "AN APPLE A DAY KEEPS THE DOCTOR AWAY", ] pdf = Document() page = Page() pdf.append_page(page) # layout layout = SingleColumnLayout(page) # add title layout.add( Paragraph( "Secret Code Puzzle", font_size=Decimal(20), font_color=X11Color("YellowGreen"), ) ) # add text layout.add( Paragraph( """ There are three riddles below written in a secret code. Can you unravel them? Once you have, copy the sentence on the line underneath the code. Hint: Each letter of the alphabet has been replaced by a number. All the riddles use the same code. """, respect_newlines_in_text=True, font_color=X11Color("SlateGray"), font_size=Decimal(8), ) ) # add grid for s in sentences: layout.add(self._build_table_for_sentence(s)) layout.add(Paragraph(" ", respect_spaces_in_text=True)) # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) # title layout.add( Paragraph( "Lissajours Line Art", font_size=Decimal(20), font_color=X11Color("Blue"), )) # table N = 7 fill_colors = [ HSVColor(Decimal(x / N), Decimal(1), Decimal(1)) for x in range(0, N) ] stroke_colors = [HSVColor.darker(x) for x in fill_colors] fixed_bb = Rectangle(Decimal(0), Decimal(0), Decimal(100), Decimal(100)) t = Table(number_of_rows=N, number_of_columns=N) for i in range(0, N): for j in range(0, N): t.add( Shape( LineArtFactory.lissajours(fixed_bb, i + 1, j + 1), fill_color=fill_colors[(i + j) % N], stroke_color=stroke_colors[(i + j) % N], line_width=Decimal(2), )) t.set_padding_on_all_cells(Decimal(10), Decimal(10), Decimal(10), Decimal(10)) layout.add(t) # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) # 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) # 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): pdf = Document() page = Page() pdf.append_page(page) # layout layout = MultiColumnLayout(page, number_of_columns=2) # # add title layout.add( Paragraph( """ Help the person find their way home by colouring in a path through the stepping stones by making a sentence. A really fun way to practice the alphabet! """, font_color=X11Color("SlateGray"), font_size=Decimal(8), )) seq = [x for x in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"] layout.add( SteppingStonePuzzle( seq, seq, "https://icons.iconarchive.com/icons/chanut/role-playing/128/King-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Castle-icon.png", )) # go to next column layout.switch_to_next_column() # add title layout.add( Paragraph( "Stepping Stones", font_size=Decimal(20), font_color=X11Color("YellowGreen"), )) # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) rs = [] for i, s in enumerate( [ "Once upon a midnight dreary,", "while I pondered weak and weary,", "over many a quaint and curious", "volume of forgotten lore", ] ): r = LineOfText( s, font_size=Decimal(20), horizontal_alignment=Alignment.CENTERED, ).layout( page, Rectangle( Decimal(20), Decimal(724 - 24 * i), Decimal(500), Decimal(24) ), ) rs.append(r) # add rectangle annotation page.append_square_annotation( stroke_color=X11Color("Salmon"), rectangle=Rectangle( Decimal(20), Decimal(724 - 24 * 3), Decimal(500), Decimal(24 * 4) ), ) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def 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): # 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 _write_maze_page(self, pdf: Document, maze_url: str, title_color: str): # add page page = Page() pdf.append_page(page) # generate maze m = SilhouetteMaze(maze_url) # add layout = SingleColumnLayout(page) # add title layout.add( Paragraph( "MAZE NR %d" % (page.get_page_info().get_page_number() + 1), font="TimesRoman", font_size=Decimal(20), font_color=HexColor(title_color), ) ) # add subtitle layout.add( Paragraph( """ Can you solve this maze? Try going from (lower) left to (upper) right. Good luck """, respect_newlines_in_text=True, font_color=X11Color("SlateGray"), font_size=Decimal(8), ) ) # add maze layout.add( DisjointShape( m.get_walls(Decimal(10)), stroke_color=HexColor(title_color), line_width=Decimal(1), horizontal_alignment=Alignment.CENTERED, vertical_alignment=Alignment.MIDDLE, ) )
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) bb = Rectangle(Decimal(20), Decimal(600), Decimal(500), Decimal(124)) Paragraph( "Once upon a midnight dreary, while I pondered weak and weary, over many a quaint and curious volume of forgotten lore", font_size=Decimal(20), vertical_alignment=Alignment.TOP, horizontal_alignment=Alignment.LEFT, padding_top=Decimal(5), padding_right=Decimal(5), padding_bottom=Decimal(5), padding_left=Decimal(5), ).layout( page, bb, ) # add rectangle annotation page.append_square_annotation( stroke_color=X11Color("Red"), rectangle=bb, ) # determine output location out_file_001 = self.output_dir / "output_001.pdf" out_file_002 = self.output_dir / "output_002.pdf" # attempt to store PDF with open(out_file_001, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) print("\nSAVING SECOND TIME\n") with open(out_file_002, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def transform( self, object_to_transform: Union[io.BufferedIOBase, io.RawIOBase, AnyPDFType], parent_object: Any, context: Optional[TransformerContext] = None, event_listeners: typing.List[EventListener] = [], ) -> Any: # update context assert context is not None assert isinstance(object_to_transform, io.BufferedIOBase) or isinstance( object_to_transform, io.RawIOBase) context.root_object = Document() context.source = object_to_transform context.tokenizer = HighLevelTokenizer(context.source) # add listener(s) for l in event_listeners: context.root_object.add_event_listener(l) # remove prefix self._remove_prefix(context) # check header self._check_header(context) # file size context.source.seek(0, os.SEEK_END) file_length = context.source.tell() context.source.seek(0) context.root_object["FileSize"] = Decimal(file_length) # build XREF object self._read_xref(context) # transform trailer dictionary xref = context.root_object.get("XRef") if "Trailer" in xref and "Encrypt" in xref["Trailer"]: # TODO raise NotImplementedError( "password-protected PDFs are currently not supported") trailer = self.get_root_transformer().transform( context.root_object["XRef"]["Trailer"], context.root_object, context, [], ) xref["Trailer"] = trailer for k in ["DecodeParms", "Filter", "Index", "Length", "Prev", "W"]: if k in xref["Trailer"]: xref["Trailer"].pop(k) # return return context.root_object
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 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 = 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) 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) for i, c in enumerate([ X11Color("Red"), X11Color("Orange"), X11Color("Yellow"), X11Color("YellowGreen"), X11Color("Blue"), X11Color("Purple"), ]): ChunkOfText("Hello World!", font_size=Decimal(24), font_color=c).layout( page, Rectangle( Decimal(100 + i * 30), Decimal(724 - i * 30), Decimal(100), Decimal(100), ), ) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() bb = Rectangle(Decimal(20), Decimal(600), Decimal(500), Decimal(124)) for va in [Alignment.TOP, Alignment.MIDDLE, Alignment.BOTTOM]: for ha in [Alignment.LEFT, Alignment.CENTERED, Alignment.RIGHT]: page = Page() pdf.append_page(page) Paragraph( "%s %s - Once upon a midnight dreary, while I pondered weak and weary, over many a quaint and curious volume of forgotten lore" % (str(va), str(ha)), font_size=Decimal(20), vertical_alignment=va, horizontal_alignment=ha, padding_top=Decimal(5), padding_right=Decimal(5), padding_bottom=Decimal(5), padding_left=Decimal(5), background_color=X11Color("Salmon"), ).layout( page, bb, ) # add rectangle annotation page.append_square_annotation( stroke_color=X11Color("Red"), rectangle=bb, ) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = MultiColumnLayout(page, number_of_columns=2) layout.add( Paragraph( "The Raven", font_size=Decimal(20), font="Helvetica-Oblique", font_color=HexColor("708090"), )) layout.add( Paragraph( """Once upon a midnight dreary, while I pondered, weak and weary, Over many a quaint and curious volume of forgotten lore- While I nodded, nearly napping, suddenly there came a tapping, As of some one gently rapping, rapping at my chamber door. 'Tis some visitor,' I muttered, 'tapping at my chamber door- Only this and nothing more.'""", text_alignment=Alignment.CENTERED, font_size=Decimal(7), respect_newlines_in_text=True, )) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) padding = Decimal(5) layout_rect = Paragraph( "Once upon a midnight dreary,\nwhile I pondered weak and weary,\nover many a quaint and curious\nvolume of forgotten lore", font_size=Decimal(20), horizontal_alignment=Alignment.CENTERED, text_alignment=Alignment.CENTERED, respect_newlines_in_text=True, padding_top=padding, padding_right=padding, padding_bottom=padding, padding_left=padding, border_right=True, border_top=True, border_color=X11Color("Green"), font_color=X11Color("Salmon"), ).layout( page, Rectangle(Decimal(20), Decimal(600), Decimal(500), Decimal(124)), ) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) t = Table(number_of_rows=3, number_of_columns=2) # row 0 t.add(Paragraph("A")) t.add(Paragraph("B")) # row 1 t.add(Paragraph(" ", respect_spaces_in_text=True)) t.add(Paragraph(" ", respect_spaces_in_text=True)) # row 2 t.add(Paragraph(" ", respect_spaces_in_text=True)) t.add(Paragraph(" ", respect_spaces_in_text=True)) t.set_padding_on_all_cells(Decimal(5), Decimal(5), Decimal(5), Decimal(5)) layout.add(t) # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page(s) N = 3 for i in range(0, N): page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) layout.add(Paragraph("Page %d of %d" % (i + 1, N))) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create 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) 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() # add page to document pdf.append_page(page) # 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