added PDF Merger

This commit is contained in:
Niklas Müller 2023-12-06 22:26:14 +01:00
parent 52dc67adc1
commit 5245997352
4 changed files with 120 additions and 44 deletions

View File

@ -5,6 +5,7 @@ FROM python:3.11-slim
WORKDIR /app WORKDIR /app
RUN mkdir /app/uploads RUN mkdir /app/uploads
RUN mkdir /app/split RUN mkdir /app/split
RUN mkdir /app/merge
RUN apt-get update RUN apt-get update
# install dependencies # install dependencies

68
app.py
View File

@ -1,26 +1,32 @@
from flask import Flask, render_template, request, redirect, url_for, jsonify, send_from_directory
import os
from PyPDF2 import PdfReader, PdfWriter
from pathlib import Path
import shutil import shutil
import os import os
from flask import Flask, render_template, request, redirect, jsonify, send_from_directory
from PyPDF2 import PdfReader, PdfWriter
from pathlib import Path
app = Flask(__name__) app = Flask(__name__)
UPLOAD_FOLDER = 'uploads' UPLOAD_FOLDER = 'uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/') @app.route('/')
def index(): def index():
return render_template('index.html') return render_template('index.html')
@app.route('/split/<path:path>') @app.route('/split/<path:path>')
def send_report(path): def send_report(path):
return send_from_directory('split', path) return send_from_directory('split', path)
@app.route('/split', methods=['POST'])
def split_file(): @app.route('/merge/<path:path>')
def send_merge(path):
return send_from_directory('merge', path)
@app.route('/split_to_zip', methods=['POST'])
def split_to_zip():
if 'pdf' not in request.files: if 'pdf' not in request.files:
return redirect(request.url) return redirect(request.url)
@ -34,14 +40,11 @@ def split_file():
filename = os.path.join(app.config['UPLOAD_FOLDER'], pdf_file.filename) filename = os.path.join(app.config['UPLOAD_FOLDER'], pdf_file.filename)
pdf_file.save(filename) pdf_file.save(filename)
out_filenames = [] out_filenames = []
Path("/tmp/split_pdf").mkdir(parents=True, exist_ok=True) Path("/tmp/split_pdf").mkdir(parents=True, exist_ok=True)
# Process the PDF file (e.g., extract text)
with open(filename, 'rb') as pdf_file: with open(filename, 'rb') as pdf_file:
pdf_reader = PdfReader(pdf_file) pdf_reader = PdfReader(pdf_file)
num_pages = len(pdf_reader.pages) num_pages = len(pdf_reader.pages)
text = ''
for page_num in range(num_pages): for page_num in range(num_pages):
writer = PdfWriter() writer = PdfWriter()
@ -63,5 +66,50 @@ def split_file():
# response.headers.add("Access-Control-Allow-Origin", "*") # response.headers.add("Access-Control-Allow-Origin", "*")
return response return response
@app.route('/merge_to_pdf', methods=['POST'])
def merge_to_pdf():
if 'pdf_1' not in request.files or 'pdf_2' not in request.files:
return redirect(request.url)
pdf_file_1 = request.files['pdf_1']
pdf_file_2 = request.files['pdf_2']
if pdf_file_1.filename == '' or pdf_file_2.filename == '':
return redirect(request.url)
if pdf_file_1:
filename_1 = os.path.join(app.config['UPLOAD_FOLDER'], pdf_file_1.filename)
pdf_file_1.save(filename_1)
if pdf_file_2:
filename_2 = os.path.join(app.config['UPLOAD_FOLDER'], pdf_file_2.filename)
pdf_file_2.save(filename_2)
if pdf_file_1 and pdf_file_2:
with open(filename_1, 'rb') as pdf_file_1, open(filename_2, 'rb') as pdf_file_2:
pdf_reader_1 = PdfReader(pdf_file_1)
pdf_reader_2 = PdfReader(pdf_file_2)
Path("/tmp/merge_pdf").mkdir(parents=True, exist_ok=True)
writer = PdfWriter()
for page_num in range(len(pdf_reader_1.pages)):
writer.add_page(pdf_reader_1.pages[page_num])
for page_num in range(len(pdf_reader_2.pages)):
writer.add_page(pdf_reader_2.pages[page_num])
out_filename = '/app/merge/merger.pdf'
with open(out_filename, 'wb') as outfile:
writer.write(outfile)
response = jsonify({"url": '/merge/merger.pdf', "name": 'merge.pdf'})
# response.headers.add("Access-Control-Allow-Origin", "*")
return response
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True) app.run(debug=True)

53
static/js/api_handler.js Normal file
View File

@ -0,0 +1,53 @@
function downloadURI(uri, name) {
var link = document.createElement("a");
link.download = name;
link.href = uri;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
delete link;
}
function uploadPDF_split() {
const fileInput = document.getElementById('split_pdfFile');
const file = fileInput.files[0];
const formData = new FormData();
formData.append('pdf', file);
const backendURL = '/split_to_zip';
fetch(backendURL, {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
console.debug("data from Backend: ", data)
downloadURI(data["url"], data["name"]);
})
.catch(error => console.error(error));
}
function uploadPDF_merge() {
const fileInput_1 = document.getElementById('merge_pdfFile_1');
const file_1 = fileInput_1.files[0];
const fileInput_2 = document.getElementById('merge_pdfFile_2');
const file_2 = fileInput_2.files[0];
const formData = new FormData();
formData.append('pdf_1', file_1);
formData.append('pdf_2', file_2);
const backendURL = '/merge_to_pdf';
fetch(backendURL, {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
console.debug("data from Backend: ", data)
downloadURI(data["url"], data["name"]);
})
.catch(error => console.error(error));
}

View File

@ -3,43 +3,17 @@
<head> <head>
<title>PDF Splitter</title> <title>PDF Splitter</title>
<link rel="shortcut icon" href="/static/icons/filetype-pdf.svg" type="image/x-icon"> <link rel="shortcut icon" href="/static/icons/filetype-pdf.svg" type="image/x-icon">
<script src="/static/js/api_handler.js"></script>
</head> </head>
<body> <body>
<h1>PDF Splitter</h1> <h1>PDF Splitter</h1>
<input type="file" id="pdfFile" accept=".pdf"><br> <input type="file" id="split_pdfFile" accept=".pdf"><br>
<button onclick="uploadPDF()">Split PDF into individual pages</button> <button onclick="uploadPDF_split()">Split PDF into individual pages</button>
<h1>PDF Merger</h1>
<input type="file" id="merge_pdfFile_1" accept=".pdf"><br>
<input type="file" id="merge_pdfFile_2" accept=".pdf"><br>
<button onclick="uploadPDF_merge()">Merge the two selected PDFs</button>
<div id="output"></div> <div id="output"></div>
<script>
function downloadURI(uri, name) {
var link = document.createElement("a");
link.download = name;
link.href = uri;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
delete link;
}
function uploadPDF() {
const fileInput = document.getElementById('pdfFile');
const file = fileInput.files[0];
const formData = new FormData();
formData.append('pdf', file);
const backendURL = '/split';
fetch(backendURL, {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
console.debug("data from Backend: ", data)
downloadURI(data["url"], data["name"]);
})
.catch(error => console.error(error));
}
</script>
</body> </body>
</html> </html>