Openhtmltopdf supports custom fonts, bold

Source: Internet
Author: User

One, support custom font

Private Static voidRenderpdf (String html, OutputStream outputstream)throwsException {Try{Pdfrendererbuilder Builder=NewPdfrendererbuilder (); AddFont (builder,"D:\\font\\"); Builder.useunicodebidisplitter (Newicubidisplitter.icubidisplitterfactory ()); Builder.useunicodebidireorderer (Newicubidireorderer ());            Builder.defaulttextdirection (TEXTDIRECTION.LTR); Builder.usesvgdrawer (Newbatiksvgdrawer ());       Builder.useobjectdrawerfactory (Buildobjectdrawerfactory ()); This section can be ignored, regular processing content (no optimization) String h= Html.replaceall ("<!--[\\w\\w\r\\n]*?-->", ""). ReplaceAll ("(? i) ]*?[ \\s\ "]) [(. *?) ). ReplaceAll ("&nbsp;", ""). ReplaceAll ("(ng-bind=\" |ng-class=\ "|ng-src=\" |ng-style=\ " )(.*?) \"", ""); Builder.withhtmlcontent (H, Testcaserunner.class. GetResource ("/testcases/"). toString ());            Builder.tostream (OutputStream);        Builder.run (); } finally{outputstream.close (); }    }


    /*** Add Font Library *@paramBuilder *@paramdir*/    Private Static voidAddFont (pdfrendererbuilder builder, String dir) {File F=NewFile (dir); if(F.isdirectory ()) {file[] files= F.listfiles (NewFilenameFilter () { Public BooleanAccept (File dir, string name) {string Lower=name.tolowercase ();//Lower.endswith (". OTf") | | There is a problem with font support for the OTF library, temporarily masking                    returnLower.endswith (". Ttf") | | Lower.endswith (". TTC"));            }            });  for(File subfile:files) {String fontFamily= Subfile.getname (). substring (0, Subfile.getname (). LastIndexOf (".")); Builder.usefont (Subfile, fontFamily); }        }    }

Second, support font bold

 for (File subfile:files) {                = subfile.getname (). substring (0, Subfile.getname (). LastIndexOf ("." ));
Core code
Custom rule Bold Library contains "_"
700 is the bold value, default is 400

if (Fontfamily.indexof ("_") > 0) { Builder.usefont (subfile, fontfamily.substring (true ); } Else { Builder.usefont (subfile, fontFamily); } }

Italic and similar

Traceability source, one font for multiple font lists (regular, bold, italic, bold italic)

According to the font name _ bold _style to determine the priority, filtering in turn

Openhtmltopdf supports custom fonts, bold

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.