jobsta/reportbro-lib

The fonts encoding is hard coded!

l327253678 opened this issue · 6 comments

self.set_doc_option('core_fonts_encoding', 'windows-1252')

encode u'你好',throw exception

txt2 = self._escape(txt.encode(self.core_fonts_encoding))
https://github.com/jobsta/pyfpdf/blob/90d32a945f15ac0c2a37fbc63ae58ee840a6237c/fpdf/fpdf.py#L689

which encoding do you want to set?

I don't think setting a different encoding will help. It would be necessary to set the used encoding in the pdf header somewhere, because the encoded string is directly written to the pdf file. e.g.
https://github.com/jobsta/pyfpdf/blob/90d32a945f15ac0c2a37fbc63ae58ee840a6237c/fpdf/fpdf.py#L690

I don't know if that is possible. Further the standard fonts need to support those characters as well (pdf comes with 5 fonts and 14 variations in total). Maybe you have more information about that. Also we have to know the width of each used characters, currently there are only 256 character widths defined for each of those fonts (maybe because the standard fonts only have 256 chars?):
https://github.com/jobsta/pyfpdf/blob/master/fpdf/fonts.py

You can always use a custom TrueType font which supports all your needed characters. A subset font will automatically be embedded by ReportBro (fpdf lib respectively).

We are trying to use ReportBro recently, but we are a little confused. In the project, we only introduced the dist folder and the contents contained, just Installing ReportBro Designer. There is no Installing ReportBro Lib. We use ajax to request data back to the background in JS. The Chinese character can be displayed normally, or the format of the design can be saved. But the preview is a hint. I did not understand the way the official gave me. I was very anxious.

I didn't use the *.py file of ReportBro. We only used the front-end part of ReportBro, and the rest were written by ourselves. Our customers need to use Chinese to transfer data from the background to ReportBro, showing no problem. There is no problem in the design format. I guess this kind of error verification is written in JS. Can you skip this verification? Or how can he get him to work?

This issue is about reportbro-lib and has nothing to do with the designer. Rendering in the designer is done by the browser, what we need is unicode support for standard fonts in the lib. Actually I've already described everything in my first message: we need to know if standard fonts in pdf support different encodings and how to set them. If this is possible we also need the character width for all characters.

So either you (or someone else) can help us get the needed information or you must use a custom font which supports your characters (only possible with own server for reportbro-lib).

Hello @alhman thank you for your reply
Here is my solution:

# In class FPDFRB
def __init__(self, document_properties, additional_fonts, custom_encoding=None):
    ...
    self.set_doc_option('core_fonts_encoding', custom_encoding or 'windows-1252')

# In class DocumentPDFRenderer
def __init__(self, header_band, content_band, footer_band, report, context,
            additional_fonts, filename, add_watermark, custom_encoding=None):
    ...
    self.pdf_doc = FPDFRB(report.document_properties, additional_fonts=additional_fonts, custom_encoding=custom_encoding)

# In class Report
def generate_pdf(self, filename='', add_watermark=False, custom_encoding=None):
    renderer = DocumentPDFRenderer(header_band=self.header,
            content_band=self.content, footer_band=self.footer,
            report=self, context=self.context,
            additional_fonts=self.additional_fonts,
            filename=filename, add_watermark=add_watermark, custom_encoding=custom_encoding)
    return renderer.render()

# User code
report_file = report.generate_pdf(custom_encoding='utf8')

a solution for what exactly? I already know how to pass a parameter. This does not solve anything, the issue is already described in my first reply and I'm not going to repeat myself for a third time.