xhtml2pdf / xhtml2pdf

A library for converting HTML into PDFs using ReportLab
https://xhtml2pdf.readthedocs.io/
Apache License 2.0
2.26k stars 641 forks source link

Cannot change the (default) font #777

Open localhostPIB opened 2 months ago

localhostPIB commented 2 months ago

Hello,

I have the problem that I cannot change the font in xhtml2pdf (I have my own font that I want to integrate).

The style rules:

html_style = f"""  
<head>
<style>

@font-face {{
    font-family: 'FiraSans';
    src: url('/static/fonts/FiraSans-Book.ttf');
}}

@page {{
    size: A4;
    @frame header_frame {{        
        -pdf-frame-content: header_content;
        left: 50pt; width: 512pt; top: 50pt; height: 40pt;
    }}
    @frame content_frame {{
        left: 50pt; width: 512pt; top: 90pt; height: 682pt;
    }}
    @frame footer_frame {{
        -pdf-frame-content: footer_content;
        left: 50pt; width: 512pt; top: 800pt; height: 20pt;
    }}
}}

body {{
    margin: 1cm;
    font-family: 'FiraSans';
}}
</style>
</head>"""

The Python functionality to assemble the HTML:


def generate_html(text):
    return f"""
            <html>
            {html_style}
                <body>
                    <div id="footer_content">Page <pdf:pagenumber> of <pdf:pagecount>
                    </div>
                {text}
                </body>
            </html>
            """

I also use the font in a CSS for an HTML page (where it also works), so the font path seems to be okay.

System Information

I have the problem on these systems: OS version: Win 10 and Ubuntu 22.04 (both 64 bit Version) Python version: 3.10 and 3.11 XHTML2PDF version: 0.2.16

I would be very pleased to receive help :-)

stefan6419846 commented 2 months ago

Is there anything logged? Additionally, you might want to use a relative path instead of an absolute one - at least in my case a relative path worked flawlessly with your example.

localhostPIB commented 2 months ago

Is there anything logged? Additionally, you might want to use a relative path instead of an absolute one - at least in my case a relative path worked flawlessly with your example.

Thanks for the feedback, unfortunately neither the relative nor the absolute path works

@font-face {{ font-family: 'FiraSans' src: url('/static/fonts/FiraSans-Book.ttf'); }}

or:

@font-face {{ font-family: 'FiraSans' src: url('Project/app'/static/fonts/FiraSans-Book.ttf'); }}

With the command I get the following information:

pdffonts '/home/User/Downloads/doku.pdf' Internal Error: xref num 1919736357 not found but needed, try to reconstruct<0a> name type encoding emb sub uni object ID


Helvetica Type 1 WinAnsi no no no 6 0 BAAAAA+DejaVuSans TrueType WinAnsi yes yes no [none] CAAAAA+LiberationSans-Bold TrueType WinAnsi yes yes yes 11 0 DAAAAA+LiberationSans TrueType WinAnsi yes yes yes yes 15 0 Helvetica Type 1 WinAnsi no no no [none]

stefan6419846 commented 2 months ago

Do you get any log output from xhtml2pdf during the build?

localhostPIB commented 2 months ago

Do you get any log output from xhtml2pdf during the build?

I use xhtml2pdf together with Flask I have activated the logging (debug) once.

Windows 10:

DEBUG:xhtml2pdf.document:pisaDocument options:
  src = '\n            <html>\n              \n<head>\n<style>\n\n@font-face {\n    font-family: \'FiraSans\', sans-serif;\n    src: url(\'/static/fonts/FiraSans-Book.ttf\') format(\'truetype\');\n}\n\n@font-face {\n    font-family: \'FiraSans-Bold\', sans-serif;\n    src: url(\'static/fonts/FiraSans-Bold.ttf\') format(\'truetype\');\n}\n\n@page {\n    size: A4;\n    @frame header_frame {        \n        -pdf-frame-content: header_content;\n        left: 50pt; width: 512pt; top: 50pt; height: 40pt;\n    }\n    @frame content_frame {\n        left: 50pt; width: 512pt; top: 90pt; height: 682pt;\n    }\n    @frame footer_frame {\n        -pdf-frame-content: footer_content;\n        left: 50pt; width: 512pt; top: 800pt; height: 20pt;\n    }\n}\n\n* {\n    font-family: \'FiraSans\';\n}\n\n</style>\n</head>\n                <body>\n                    <div id="footer_content">Page <pdf:pagenumber> of <pdf:pagecount>\n                    </div>\n                        <p>Lorem ipsum text....</p>\n\n                </body>\n            </html>\n            '
  dest = <_io.BytesIO object at 0x00000193AC6395E0>
  path = ''
  link_callback = None
  xhtml = False
  context_meta = None
DEBUG:xhtml2pdf.files:FileObject '/static/fonts/FiraSans-Book.ttf', Basepath: 'C:\\Users\\User\\Desktop\\Project\\__dummy__'
DEBUG:xhtml2pdf.files:URLParts: ParseResult(scheme='c', netloc='', path='\\Users\\User\\Desktop\\Project\\__dummy__', params='', query='', fragment=''), 'c'
DEBUG:xhtml2pdf.files:Unrecognized scheme, assuming local file path
DEBUG:xhtml2pdf.files:FileObject 'static/fonts/FiraSans-Bold.ttf', Basepath: 'C:\\Users\\User\\Desktop\\Project\\__dummy__'
DEBUG:xhtml2pdf.files:URLParts: ParseResult(scheme='c', netloc='', path='\\Users\\User\\Desktop\\Project\\__dummy__', params='', query='', fragment=''), 'c'
DEBUG:xhtml2pdf.files:Unrecognized scheme, assuming local file path