no-more-plag-v2 / app.py
ayush-thakur02's picture
Update app.py
190419b verified
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):
"""Generate random gibberish consisting of alphanumeric characters and symbols."""
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
return ''.join(gibberish_chars)
def add_invisible_text_to_pdf(input_pdf_bytes, num_pages_to_modify=5):
import tempfile
# 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 with full transparency)
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
y_position = height - 10 # Start near the top of the page
line_height = 8 # Space between lines
# Calculate how many characters fit in one line across the full width
# Average character width for Helvetica at size 6 is approximately 3.6 points
chars_per_line = int((width - 20) / 3.6) # 20 points margin (10 on each side)
# Generate enough gibberish to cover the entire page
total_chars_needed = chars_per_line * int((height - 20) / line_height)
gibberish = generate_random_gibberish(length=total_chars_needed)
# Split gibberish into lines that fit the full width
char_index = 0
while y_position > 10: # Keep going until we reach the bottom
if char_index >= len(gibberish):
# Generate more gibberish if needed
gibberish += generate_random_gibberish(length=total_chars_needed)
# Extract a line of text that fits the width
line = gibberish[char_index:char_index + chars_per_line]
c.drawString(10, y_position, line) # Draw the text from left edge
y_position -= line_height # Move down for the next line
char_index += chars_per_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=200, 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 across the entire width of the first few pages."
)
# Launch the app
iface.launch()