#!/usr/bin/env python3 """Write a fake scanned invoice, as a PDF with no text layer, to stdout. It draws a one-page invoice, tilts it a little, adds speckle and blur like a cheap scanner, and wraps the image in a PDF. Paperless has to OCR it to find any words. Run it inside the Paperless container, which has Pillow and img2pdf: docker compose exec -T webserver python3 - < make-test-scan.py > test-scan.pdf """ import glob import io import random import sys import img2pdf from PIL import Image, ImageDraw, ImageFilter, ImageFont W, H = 2480, 3508 # A4 at 300 dpi fonts = sorted(glob.glob("/usr/share/fonts/**/*.ttf", recursive=True)) def font(size): return ImageFont.truetype(fonts[0], size) if fonts else ImageFont.load_default(size) lines = [ (90, "Riverside Plumbing Co."), (48, "118 Mill Street, Portland OR 97205"), (48, ""), (70, "INVOICE No. 4711"), (48, "Date: 3 November 2026"), (48, ""), (48, "Bill to: Jordan Example, 42 Elm Avenue"), (48, ""), (52, "Water heater replacement (50 gal) $1,040.00"), (52, "Expansion tank and valves $164.50"), (52, "Labor, 2 hours $80.00"), (48, ""), (64, "Total due: $1,284.50"), (48, ""), (44, "Payment due within 30 days. Thank you for your business."), ] img = Image.new("L", (W, H), 245) draw = ImageDraw.Draw(img) y = 250 for size, text in lines: draw.text((220, y), text, fill=20, font=font(size)) y += int(size * 1.9) img = img.rotate(1.3, fillcolor=235, resample=Image.BICUBIC) px = img.load() for _ in range(60000): px[random.randrange(W), random.randrange(H)] = random.randrange(120, 230) img = img.filter(ImageFilter.GaussianBlur(1.1)) jpeg = io.BytesIO() img.save(jpeg, "JPEG", quality=70, dpi=(300, 300)) sys.stdout.buffer.write(img2pdf.convert(jpeg.getvalue()))