import ipywidgets as widgets
pdf_uploader = widgets.FileUpload(description="Original pdfs", accept=".pdf", multiple=True)
tag_uploader = widgets.FileUpload(description="Tag image/pdf", accept="image/*,.pdf", multiple=False)
output = widgets.Output()
@output.capture()
def write_pdf(pdfs):
for pdf in pdfs:
print(f"Loading {pdf.name}...", end=" ")
with open(pdf.name, "wb") as f:
f.write(pdf.content)
print("done")
def write_original_pdf(counter):
if counter.get("new") == 0:
return
write_pdf(pdf_uploader.value)
def write_tag_pdf(counter):
if counter.get("new") == 0:
return
write_pdf(tag_uploader.value)
pdf_uploader.observe(write_original_pdf, names="value")
tag_uploader.observe(write_tag_pdf, names="value")
def clear(_):
output.clear_output()
pdf_uploader._counter = 0
tag_uploader._counter = 0
pdf_uploader.value = tuple()
tag_uploader.value = tuple()
kwargs = dict(min=0.0, max=1.0, step=0.01)
xpos = widgets.FloatSlider(value=0.5, description="$x$", **kwargs)
ypos = widgets.FloatSlider(value=0.5, description="$y$", **kwargs)
width = widgets.FloatSlider(value=0.7, description="width", **kwargs)
@output.capture()
def process(_):
import subprocess
if len(pdf_uploader.value) == 0:
print("Missing file to tag!")
return
if len(tag_uploader.value) == 0:
print("Missing tag!")
return
tag_name = tag_uploader.value[0].name
for pdf in pdf_uploader.value:
print(f"Processing {pdf.name}...", end=" ")
result = subprocess.run(
[
"./bin/generate_tag.sh",
"--multi",
pdf.name,
"--tag",
tag_name,
"-x",
str(xpos.value),
"-y",
str(ypos.value),
"-w",
str(width.value),
],
capture_output=True,
text=True,
)
if result.returncode != 0:
print("failed!")
print("Check the following messages to know what's going on:")
print(result.stdout)
print(result.stderr)
else:
print("done")
@output.capture()
def download(_):
from IPython.display import HTML
for pdf in pdf_uploader.value:
pdf_name = pdf.name.replace(" ", "_")
js_code = f"""
var a = document.createElement('a');
var href = window.location.href;
n = href.includes('gesis') ? 7 : 5;
var url = href.split('/').slice(0, n).join("/");
a.setAttribute('download','');
a.setAttribute('href', url + '/files/bin/pdf/{pdf_name}');
a.click()
"""
with output:
display(HTML(f"<script>{js_code}</script>"))
process_button = widgets.Button(description="Process", icon="check", button_style="success")
download_button = widgets.Button(description="Download", icon="download", button_style="warning")
clear_button = widgets.Button(description="Clear", icon="trash", button_style="danger")
clear_button.on_click(clear)
download_button.on_click(download)
process_button.on_click(process)
top = widgets.VBox(
[
widgets.HTML(value="Tag position/width in fraction of the page"),
widgets.HBox([widgets.HTML(value="Select pdf(s) to tag"), pdf_uploader, xpos]),
widgets.HBox([widgets.HTML(value="Select tag image"), tag_uploader, ypos]),
width,
],
layout=widgets.Layout(display="flex", align_items="flex-end"),
)
bottom = widgets.HBox([process_button, download_button, clear_button])
widgets.HBox(
[
widgets.HTML(value="<h1>Tag Application</h1>"),
top,
widgets.HTML(value="<p style='margin-bottom:1cm;'></p>"),
bottom,
widgets.VBox([widgets.HTML(value="<h2>Logs</h2>"), output]),
],
layout=widgets.Layout(display="flex", flex_flow="column", align_items="center", width="100%"),
)