Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pdfitdown.pdfconversion import convert_to_pdf, convert_markdown_to_pdf
|
| 2 |
+
import warnings
|
| 3 |
+
from typing import List
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
class FileNotConvertedWarning(Warning):
|
| 7 |
+
"""The file was not in one of the specified formats for conversion to PDF,thus it was not converted"""
|
| 8 |
+
|
| 9 |
+
def to_pdf(files: List[str]) -> List[str]:
|
| 10 |
+
"""
|
| 11 |
+
Converts various file formats to PDF.
|
| 12 |
+
|
| 13 |
+
Args:
|
| 14 |
+
files: List of file paths to convert. Supports .docx, .pdf, .html, .pptx,
|
| 15 |
+
.csv, .xml, and .md files.
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
List of paths to converted PDF files. For files already in PDF format,
|
| 19 |
+
returns original path.
|
| 20 |
+
|
| 21 |
+
Raises:
|
| 22 |
+
FileNotConvertedWarning: When file format is not supported.
|
| 23 |
+
"""
|
| 24 |
+
pdfs = []
|
| 25 |
+
for f in files:
|
| 26 |
+
if f.endswith(".docx"):
|
| 27 |
+
newfile = f.replace(".docx", ".pdf")
|
| 28 |
+
file_to_add = convert_to_pdf(f, newfile, newfile.split(".")[0])
|
| 29 |
+
pdfs.append(file_to_add)
|
| 30 |
+
elif f.endswith(".pdf"):
|
| 31 |
+
pdfs.append(f)
|
| 32 |
+
elif f.endswith(".html"):
|
| 33 |
+
newfile = f.replace(".html", ".pdf")
|
| 34 |
+
file_to_add = convert_to_pdf(f, newfile, newfile.split(".")[0])
|
| 35 |
+
pdfs.append(file_to_add)
|
| 36 |
+
elif f.endswith(".pptx"):
|
| 37 |
+
newfile = f.replace(".pptx", ".pdf")
|
| 38 |
+
file_to_add = convert_to_pdf(f, newfile, newfile.split(".")[0])
|
| 39 |
+
pdfs.append(file_to_add)
|
| 40 |
+
elif f.endswith(".csv"):
|
| 41 |
+
newfile = f.replace(".csv", ".pdf")
|
| 42 |
+
file_to_add = convert_to_pdf(f, newfile, newfile.split(".")[0])
|
| 43 |
+
pdfs.append(file_to_add)
|
| 44 |
+
elif f.endswith(".xml"):
|
| 45 |
+
newfile = f.replace(".xml", ".pdf")
|
| 46 |
+
file_to_add = convert_to_pdf(f, newfile, newfile.split(".")[0])
|
| 47 |
+
pdfs.append(file_to_add)
|
| 48 |
+
elif f.endswith(".md"):
|
| 49 |
+
newfile = f.replace(".md", ".pdf")
|
| 50 |
+
file_to_add = convert_markdown_to_pdf(f, newfile, newfile.split(".")[0])
|
| 51 |
+
pdfs.append(file_to_add)
|
| 52 |
+
else:
|
| 53 |
+
warnings.warn(f"File {f} was not converted to PDF because its file format is not included in those that can be converted", FileNotConvertedWarning)
|
| 54 |
+
continue
|
| 55 |
+
return pdfs
|
| 56 |
+
|
| 57 |
+
def convert(file: str) -> str:
|
| 58 |
+
files = [file]
|
| 59 |
+
pdfs = to_pdf(files)
|
| 60 |
+
return pdfs[0]
|
| 61 |
+
|
| 62 |
+
iface = gr.Interface(
|
| 63 |
+
fn=convert,
|
| 64 |
+
inputs=gr.File(label="Upload your file"),
|
| 65 |
+
outputs=gr.File(label="Converted PDF"),
|
| 66 |
+
title="File to PDF Converter",
|
| 67 |
+
description="Upload a file in .docx, .pdf, .html, .pptx, .csv, .xml, or .md format, and get it converted to PDF."
|
| 68 |
+
)
|
| 69 |
+
iface.launch()
|