File size: 1,395 Bytes
2ad8b75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fpdf import FPDF
from fpdf.html import HTMLMixin
from bs4 import BeautifulSoup
from io import BytesIO


class MyFPDF(FPDF, HTMLMixin):
    def header(self):
        # Add a border around the page
        self.rect(5.0, 5.0, self.w - 10.0, self.h - 10.0)

class Pdf:
    def __init__(self):
        self.pdf = MyFPDF()

    def replace_special_characters(self, text):
        replacements = {
            '’': "'",
            'β€œ': '"',
            '”': '"',
            '–': '-',
            'β€”': '-',
            '…': '...',
        }
        for original, replacement in replacements.items():
            text = text.replace(original, replacement)
        return text

    def pdf_creator(self, html_text):
        self.pdf.add_page()
    
        # Use the built-in Arial font
        self.pdf.set_font("Times", size=12)
       
        soup = BeautifulSoup(html_text, 'html.parser')

          # Replace special characters in the text
        for element in soup.find_all(text=True):
            element.replace_with(self.replace_special_characters(element))

        # Convert entire HTML content to PDF
        self.pdf.write_html(str(soup))

        # Save PDF to bytes object
        output = BytesIO()
        self.pdf.output(output)
        pdf_bytes = output.getvalue()
        output.close()

        return pdf_bytes