3This script creates a 400×400 thumbnail of a specific page of a PDF file.
5It uses similar thumbnailing logic to docstore [1], but it allows me to
6pick a particular page rather than the first page.
8This is helpful because sometimes I download PDF cross-stitch patterns
9where the first page contains text which isn't a good thumbnail, but
10later pages do show the whole pattern.
12[1]: https://github.com/alexwlchan/docstore/blob/main/src/docstore/thumbnails.py
22from pypdf import PdfReader, PdfWriter
26 parser = argparse.ArgumentParser(
27 prog=os.path.basename(__file__),
28 description="Get thumbnails from a specific page of a PDF file.",
31 parser.add_argument("PATH")
35 metavar="PAGE_NUMBER",
36 help="which page of the PDF to get",
40 "--width", type=int, help="pixel width of the generated thumbnail", default=400
43 return parser.parse_args(argv)
46if __name__ == "__main__":
47 args = parse_args(sys.argv[1:])
49 reader = PdfReader(args.PATH)
51 # Remember that pages are 0-indexed
53 page = reader.pages[args.page - 1]
55 sys.exit(f"Unrecognised page: {args.page}, expected 1...{len(reader.pages)}")
60 with tempfile.TemporaryDirectory(suffix=".pdf") as temp_dir:
61 out_path = os.path.join(temp_dir, os.path.basename(args.PATH))
63 with open(out_path, "wb") as out_file:
64 writer.write(out_file)
66 subprocess.check_call(
67 ["qlmanage", "-t", out_path, "-s", f"{args.width}x{args.width}", "-o", "."]