| skipped 3 lines |
4 | 4 | | import argparse |
5 | 5 | | import binascii |
6 | 6 | | import colorama |
7 | | - | import pypdf |
8 | 7 | | import io |
9 | 8 | | |
10 | 9 | | from colorama import Fore, Back, Style |
11 | 10 | | from PIL import Image, ImageDraw, ImageFont |
| 11 | + | from reportlab.pdfgen import canvas |
| 12 | + | from reportlab.lib.pagesizes import B5 |
12 | 13 | | |
13 | | - | def make_png_data(text, byte_size): |
14 | | - | image = Image.new('RGB', (516, 729), (255, 255, 255)) # B5, White |
| 14 | + | def make_png(file_path, text, byte_size): |
| 15 | + | image = Image.new('RGB', (729, 516), (255, 255, 255)) # B5, White |
15 | 16 | | draw = ImageDraw.Draw(image) |
16 | 17 | | draw.text((10, 10), text, fill=(0, 0, 0)) # Black |
17 | 18 | | output = io.BytesIO() |
| skipped 1 lines |
19 | 20 | | png_data = output.getvalue() |
20 | 21 | | |
21 | 22 | | if len(png_data) > byte_size: |
22 | | - | with open('dummy.png', 'wb') as f: |
| 23 | + | with open(file_path, 'wb') as f: |
23 | 24 | | f.write(png_data) |
24 | | - | return None |
| 25 | + | return False |
25 | 26 | | |
26 | 27 | | # IEND chunk is the last chunk of a PNG file |
27 | 28 | | iend_type_index = png_data.find(b'IEND') |
28 | 29 | | if iend_type_index == -1: |
29 | 30 | | print(Fore.RED + 'Error: IEND chunk not found.') |
30 | | - | return None |
| 31 | + | return False |
31 | 32 | | |
32 | 33 | | # The first 4 bytes of the IEND chunk are data length |
33 | 34 | | # IEND chunk type (4 bytes) follows IEND chunk data length (4 bytes) |
| skipped 2 lines |
36 | 37 | | # 12 = 4 (chunk length) + 4 (chunk type) + 4 (chunk CRC) |
37 | 38 | | extra_chunk_length = byte_size - len(png_data) - 12 |
38 | 39 | | # private chunk type |
39 | | - | extra_chunk_type = b'exTr' |
| 40 | + | extra_chunk_type = b'eXtr' |
40 | 41 | | extra_chunk_data = b'\x00' * extra_chunk_length |
41 | 42 | | extra_chunk_crc = binascii.crc32(extra_chunk_type + extra_chunk_data) |
42 | 43 | | extra_chunk = extra_chunk_length.to_bytes(4, byteorder='big') |
| skipped 1 lines |
44 | 45 | | extra_chunk += extra_chunk_crc.to_bytes(4, byteorder='big') |
45 | 46 | | |
46 | 47 | | added_png_data = png_data[:iend_chunk_index] + extra_chunk + png_data[iend_chunk_index:] |
47 | | - | return added_png_data |
| 48 | + | with open(file_path, 'wb') as f: |
| 49 | + | f.write(added_png_data) |
| 50 | + | return True |
| 51 | + | |
| 52 | + | def make_pdf(file_path, text): |
| 53 | + | c = canvas.Canvas(file_path, bottomup=False, pagesize=B5) |
| 54 | + | c.setFont('Helvetica', 30) |
| 55 | + | c.drawString(15, 40, text) |
| 56 | + | c.showPage() |
| 57 | + | c.save() |
48 | 58 | | |
49 | 59 | | def parse_args(): |
50 | 60 | | colorama.init(autoreset=True) |
51 | 61 | | parser = argparse.ArgumentParser(description='Create a dummy file of the specified size.') |
52 | | - | parser.add_argument('-type', '--type', help='File type', choices=['png', 'pdf', 'txt'], required=True) |
53 | | - | parser.add_argument('-text', '--text', help='Text to be written in the file') |
54 | | - | parser.add_argument('-size', '--size', help='File size (bytes)', type=int) |
| 62 | + | parser.add_argument('-n', '--name', help='File name(.png, .pdf)', required=True) |
| 63 | + | parser.add_argument('-t', '--text', help='Text to be written in the file') |
| 64 | + | parser.add_argument('-s', '--size', help='Bytes of file(png only)', type=int) |
55 | 65 | | args = parser.parse_args() |
56 | | - | if args.type == 'png': |
57 | | - | added_png_data = make_png_data(args.text, args.size) |
58 | | - | with open('dummy.png', 'wb') as f: |
59 | | - | f.write(added_png_data) |
60 | | - | else: |
61 | | - | print(Fore.RED + 'Error: Invalid file type.') |
62 | | - | parser.print_help() |
| 66 | + | # if args.name.endswith('.png'): |
63 | 67 | | |
64 | 68 | | if __name__ == '__main__': |
65 | 69 | | colorama.init(autoreset=True) |
66 | | - | png_data = make_png_data('sample png', 400001) |
67 | | - | with open('dummy.png', 'wb') as f: |
68 | | - | f.write(png_data) |
| 70 | + | make_pdf('dummy.pdf', 'sample pdf') |
69 | 71 | | |