File size: 2,237 Bytes
9c90775
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os



import logging
from exception import MyException
from PIL import Image
from fpdf import FPDF
from docx import Document





# ------------------ Data Conversion ------------------


async def image_to_pdf(imge_path:str,output_pdf_path:str):
    try:
        image=Image.open(imge_path)

        img=image.convert("RGB")

        img.save(output_pdf_path,"PDF")

    except Exception as e:
        raise MyException(e)
    

async def text_to_pdf(file_path:str,output_file_path:str):
    try:
        pdf = FPDF()
        pdf.add_page()
        pdf.set_font("Arial", size=12)
        
        with open(file_path, "r", encoding="utf-8") as f:
            for line in f:
                # Replace common non-latin1 characters that fpdf (standard) can't handle
                line = line.replace('\u2018', "'").replace('\u2019', "'").replace('\u201c', '"').replace('\u201d', '"')
                pdf.cell(200, 10, txt=line.strip(), ln=1)
                
        pdf.output(output_file_path)

    except Exception as e:
        raise MyException(e)



async def txt_to_pdf(txt_path, output_pdf_path):
    try:
        pdf = FPDF()
        pdf.add_page()
        pdf.set_font("Arial", size=12)
        
        with open(txt_path, "r", encoding="utf-8") as f:
            for line in f:
                pdf.cell(200, 10, txt=line.strip(), ln=1)
                
        pdf.output(output_pdf_path)

    except Exception as e:
        raise MyException(e)    



async def docs_to_pdf(docs_path: str, output_pdf_path: str):
    try:
        doc = Document(docs_path)

        pdf = FPDF()
        pdf.add_page()
        pdf.set_font("Arial", size=12)

        for para in doc.paragraphs:
            line = para.text.strip()
            line = (
                line
                .replace('\u2018', "'")
                .replace('\u2019', "'")
                .replace('\u201c', '"')
                .replace('\u201d', '"')
                .replace('\u2013', '-')
                .replace('\u2014', '--')
            )
            if line:
                pdf.multi_cell(0, 8, txt=line)
            else:
                pdf.ln(4)

        pdf.output(output_pdf_path)

    except Exception as e:
        raise MyException(e)