signintech / gopdf

A simple library for generating PDF written in Go lang
MIT License
2.58k stars 280 forks source link

support for bold font style #48

Open Tom-Kail opened 7 years ago

Tom-Kail commented 7 years ago

If I embed both font of bold and normal, the size of PDF will larger than only embed one font. I want to embed one font only to shrink file size and get bold and normal font style by code. Currently the package only support "" and "U" font style, could you support bold font style or tell me how can I do to make a pr. ps: the project is awesome! you've done a great work!

oneplus1000 commented 7 years ago

As far as I know, separate font files are needed for difference weight. However, file size will not be that large since only the glyphs that actually use will be embedded in the file.

Tom-Kail commented 7 years ago

Thank you to your quick response. Yes, it's not very large. We need both bold and normal font style to generate contract. For example, the PDF file generated by gopdf which embed two font files is 204 KB, but itextpdf (java) can shrink PDF file to 102 KB by only use one font and get bold font style by code. We love golang and want to use it to replace all java code. We even hope golang can have a better performance in file size(Please forgive my greed). We have millions of users and the number is growing, so we want contract files can be as small as possible.

oneplus1000 commented 7 years ago

Can you send me a sample pdf file that created from itextpdf.

Tom-Kail commented 7 years ago

https://zcmlccontract.zcmlc.com/TransferPDF/2020/20200206/hgypdf/pt_xwyswsqtxwuwyvxutrt.pdf Don't click it directly, please copy it to address bar first.

oneplus1000 commented 7 years ago

Did you use the code like this to bold the font ( in itextpdf )?

 Chunk chunk = new Chunk("ABCDE", arial12);  
 chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0.5f, null);
Tom-Kail commented 7 years ago

I only use code to create bold font:

public static final Font getChineseSectionFont() {
        Font FontChinese = null;
        try {
            BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            FontChinese = new Font(bfChinese, 12, Font.BOLD);
        } catch (DocumentException de) {
            System.err.println(de.getMessage());
        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        }
        return FontChinese;
    }

then use it like:

content = new Paragraph("一、借款", PDFFront.getChineseSectionFont());
vantaboard commented 5 months ago

@oneplus1000 can you close this issue if it has been resolved?