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 (" ", ""). 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