itext pdf 在 java 中创建:当从 localhost 进行服务调用时中文不工作,但从 main 方法调用时工作正常

itext pdf creating in java: Chinese not working when a service call is made from localhost but works fine when called from main method

我正在尝试创建包含中文字符的 pdf。 当我从 main 方法调用 generatePdf()` 时,它按预期工作。显示屏幕截图。但是,当我在 weblogic 服务器中部署并从浏览器调用“http://localhost:7001/PdfGeneration/itext/genpdf”时,它不应用该字体。我附上了屏幕截图。

以下设置:

style.css 仅包含此

body {
    font-family: "arial unicode ms";
}

代码:

@Path("itext")
    @Api(value = "itext service")
    public class iTextService {

//creating an object in main and calling the method works fine
//this part is commented when calling form server (localhost)
        public iTextService() throws Exception {
            generatePdf();        
        }

        public static void main(String args[]) throws Exception {
            iTextService obj = new iTextService();
            return;
        }


     @GET
        @Path("genpdf")
        @Produces("application/pdf")
        public void generatePdf() throws Exception {        

            ByteArrayOutputStream out = new ByteArrayOutputStream();        
            Document doc = new Document(PageSize.A4, 40, 40, 20, 10);        
            PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("testPDF.pdf"));

            doc.open();
            parseHTML(writer, doc);
            doc.close();            

        }
    //this method gives the artifact path
    // screen shot of the artifact is shown
    public String getFilePath() {
            URL url = getClass().getClassLoader().getResource("/resource/");
            String path = url.getPath();
            try {
                path = URLDecoder.decode(path, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            path = new File(path).getPath();
            return path;
    }

     public void parseHTML(PdfWriter writer, Document document) throws Exception {

            //comment this when calling from main method
            String pathToRes = getFilePath();        

            //case 1 : calling from main method
            //byte[] encoded = Files.readAllBytes("style.css"));
            //case 2: calling from browser (localhost)
            byte[] encoded = Files.readAllBytes(Paths.get(pathToRes + "\style.css"));
            String style = new String(encoded);

            CSSResolver cssResolver = new StyleAttrCSSResolver();
            CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(style.getBytes()));
            cssResolver.addCss(cssFile);

            // HTML
            XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);     
            //case 1
            //fontProvider.register("ARIALUNI.ttf"); 
            //case 2
            fontProvider.register(pathToRes + "\ARIALUNI.ttf");


            //FontFactory.register(pathToFont + "\ARIALUNI.ttf");
            //FontFactory.setFontImp(fontProvider); //tried with these two along with exisitng code once

            CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
            HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
            htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

            // Pipelines
            PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
            HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
            CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

            // XML Worker
            XMLWorker worker = new XMLWorker(css, true);
            XMLParser parser = new XMLParser(worker);        
            parser.parse(new ByteArrayInputStream("<body><p>篆書 test</p></body>".getBytes()), Charset.forName("UTF-8"));
        }

    }

screenshots

我发现了问题,我使用了 itext 提供的解析函数,它需要 Inputstream,如下所示

public void parse(InputStream in, Charset charSet) throws IOException { this.charset = charSet; InputStreamReader reader = new InputStreamReader(in, charSet); this.parse((Reader)reader); }

最初我是这样做的

XMLParser parser = new XMLParser(worker); parser.parse(new ByteArrayInputStream(buildTemplate().getBytes("UTF-8")), Charset.forName("UTF-8"));

现在我创建了一个 Inputstream,然后我将它传递给解析器并且它起作用了。

InputStream is = new ByteArrayInputStream(buildTemplate().getBytes("UTF-8")); parser.parse(is, Charset.forName("UTF-8"));

出于某种原因,第一种方法在本地有效,但在托管时无效。