Spaces:
Sleeping
Sleeping
File size: 4,045 Bytes
4aee311 1d499ae 4aee311 1d499ae 4aee311 f3f0ed6 d115401 f3f0ed6 4aee311 d115401 4aee311 d115401 4aee311 f3f0ed6 4aee311 d13d6e3 f3f0ed6 d6800a9 4aee311 f3f0ed6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
import gradio as gr
import PyPDF2
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
import random
import string
import io
def generate_random_gibberish(length=1000, line_length=100):
"""Generate random gibberish consisting of alphanumeric characters and symbols, split into lines of 100 chars."""
chars = string.ascii_letters + string.digits + string.punctuation
gibberish_chars = []
i = 0
while i < length:
# Generate word length between 3-5 characters
word_length = random.randint(3, 5)
# Generate a "word" of random characters
word = ''.join(random.choice(chars) for _ in range(word_length))
gibberish_chars.append(word)
# Add space after the word (except for the last word)
if i + word_length < length:
gibberish_chars.append(' ')
i += word_length + 1 # +1 for the space
# Join all characters and words
gibberish = ''.join(gibberish_chars)
# Split the gibberish into lines of specified length
return [gibberish[i:i+line_length] for i in range(0, len(gibberish), line_length)]
def add_invisible_text_to_pdf(input_pdf_bytes, num_pages_to_modify=5):
import tempfile
import os
# Create a file-like object from the uploaded PDF bytes
input_pdf_path = io.BytesIO(input_pdf_bytes)
reader = PyPDF2.PdfReader(input_pdf_path)
writer = PyPDF2.PdfWriter()
# Loop through the pages of the existing PDF
for page_num in range(len(reader.pages)):
page = reader.pages[page_num]
# If this page is in the set of pages we want to modify
if page_num < num_pages_to_modify:
packet = io.BytesIO()
# Create a new PDF to overlay on the existing one
c = canvas.Canvas(packet, pagesize=letter)
# Set invisible text color (white)
c.setFillColorRGB(1, 1, 1, 0) # White color (invisible on white background)
c.setFont("Helvetica", 6) # Use a smaller font for better coverage
width, height = letter # Get the page dimensions
# Generate enough gibberish to cover the entire page
gibberish_lines = generate_random_gibberish(length=10000, line_length=100)
y_position = height - 10 # Start near the top of the page
# Loop through the text and write it line by line
for line in gibberish_lines:
if y_position < 0: # If we go past the bottom of the page, stop
break
c.drawString(10, y_position, line) # Draw the text starting from left to right
y_position -= 8 # Move down for the next line
c.save()
# Move the "packet" to the start of the file and merge it with the original PDF
packet.seek(0)
overlay_pdf = PyPDF2.PdfReader(packet)
overlay_page = overlay_pdf.pages[0]
# Merge the overlay page with the original page
page.merge_page(overlay_page)
# Add the (modified or original) page to the writer
writer.add_page(page)
# Save the new PDF to a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
writer.write(tmp_file)
tmp_file_path = tmp_file.name
return tmp_file_path
# Define Gradio interface
def gradio_app(input_pdf, num_pages_to_modify):
output_pdf = add_invisible_text_to_pdf(input_pdf, num_pages_to_modify)
return output_pdf
# Create the Gradio interface
iface = gr.Interface(
fn=gradio_app,
inputs=[
gr.File(label="Upload PDF", type="binary"),
gr.Slider(minimum=1, maximum=20, value=5, label="Number of Pages to Modify")
],
outputs=gr.File(label="Output PDF"),
title="PDF Gibberish Inserter",
# description="Upload a PDF and this app will add invisible gibberish text to the first few pages. You can choose how many pages to modify."
)
# Launch the app
iface.launch() |