当前位置: 首页 > 知识库问答 >
问题:

JasperReports库自定义可视化无法正确呈现UTF8组件

唐利
2023-03-14

我正在使用JasperReports库从自定义Java应用程序生成PDF。然而,当在自定义可视化组件中呈现欧元符号时,生成的pdf显示的是一个符号(PhantomJs和Chromium都出现了这种情况)。报告(文本字段)中的其余欧元符号都正确显示了,尽管它们都使用了相同的字体。

另一方面,如果使用PhantomJs从JasperStudio呈现相同的报表,则显示正确的符号。

我已经检查了传递给chrome的html文件,因为它暂时存储在temp文件夹中,并且它似乎正确地用UTF-8编码(见下文),所以我认为这可能是一个与jasper提供给chrome或PhantomJS的字体文件有关的问题。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <link rel="stylesheet" type="text/css" href="jr_script_3615012621213017572_feeRenderer.css" title="Style">
  <script src="jr_res_12941692111646429500_require.js"></script>
  <script src="jr_res_14211739430443032080_cv-component_static.js"></script>
  <script src="jr_res_10307871901394985341_feeRenderer.min.js"></script>
</head>

<body>
  <div id="element423553011047886097313196921978673211730" class="jr_cvc_element" style="width: 389px; height: 290px;" />
  <script class="jasperreports">
    require(["cv-component"], function(Comp) {
      new Comp({
        "id": "element423553011047886097313196921978673211730",
        "renderer": "d3Circle",
        "instanceData": {
          "series": [
            [{
              "Currency_symbol": "€",
              "Currency_height": "0.5",
              "Cents_height": "0.25",
              "Separation": "0",
              "Cents": "0",
              "Currency_margin": "0",
              "Units_height": "1",
              "Units": "8",
              "Margin": "0"
            }]
          ],
          "module": "d3Circle",
          "css_uri": "D:\\Dhom\\Documents\\Projects\\Myreport\\Test5\\feeRenderer.css",
          "width": 389,
          "id": "element423553011047886097313196921978673211730",
          "height": 290,
          "script_uri": "D:\\Dhom\\Documents\\Projects\\Myreport\\Test5\\feeRenderer.min.js",
          "animation": false
        }
      });
    });
  </script>
</body>

</html>

以前有人面临过这个问题吗?

提前致谢

<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>
<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"><svg xmlns=\ "http://www.w3.org/2000/svg\" id=\ "element66576560284652498011372360525284671058svg\" width=\ "389\" height=\ "240\" style=\ "fill: transparent;\" version=\ "1.1\"><defs><style type=\"text/css\">\nsvg circle { fill: rgb(240, 0, 255); }\n.number-text { fill: white; font-family: arial; }</style></defs><text dominant-baseline=\"hanging\" aligment-baseline=\"hanging\" x=\"389\" y=\"0\" text-anchor=\"end\" class=\"number-text\" style=\"font-size: 60px;\">.95</text><text dominant-baseline=\"hanging\" aligment-baseline=\"hanging\" x=\"305.578125\" y=\"0\" text-anchor=\"end\" class=\"number-text\" style=\"font-size: 240px;\">8</text><text dominant-baseline=\"hanging\" aligment-baseline=\"hanging\" x=\"172.09375\" y=\"0\" text-anchor=\"end\" class=\"number-text\" style=\"font-size: 120px;\">\ufffd</text></svg>

通过记录包含货币符号的变量,看起来javascript解释器正在更改它。因此,如果报表被导出为html并在Chrome中打开,它将得到正确的呈现,但是当javascript代码被解释为pdf报表生成例程的一部分时,解释器无法识别该字符并将其更改为Unicode替换字符。该变量的日志值显示在java程序下面提供的日志中的第10547行:

3587 [main] DEBUG net.sf.jasperreports.customvisualization.export.ChromeCVElementImageDataProvider  - wrote CV render HTML page to C:\Users\Dhom\AppData\Local\Temp\cv_1203404104433554074.html
3589 [main] DEBUG net.sf.jasperreports.chrome.BrowserService  - page evaluation at file:/C:/Users/Dhom/AppData/Local/Temp/cv_1203404104433554074.html
3595 [main] INFO net.sf.jasperreports.chrome.ChromeInstance  - Launching Chrome instance 1 with configuration executable: D:\Dhom\Documents\Projects\Myreport\Test5\chrome.exe, headless: true, arguments: {}, idle timeout: 900000, live timeout: 7200000
3601 [main] INFO com.github.kklisura.cdt.launch.ChromeLauncher  - Launching chrome process D:\Dhom\Documents\Projects\Myreport\Test5\chrome.exe with arguments {no-first-run=true, remote-debugging-port=0, mute-audio=true, disable-client-side-phishing-detection=true, disable-popup-blocking=true, disable-default-apps=true, disable-extensions=true, metrics-recording-only=true, no-default-browser-check=true, disable-background-timer-throttling=true, disable-translate=true, safebrowsing-disable-auto-update=true, headless=true, hide-scrollbars=true, disable-background-networking=true, disable-prompt-on-repost=true, user-data-dir=C:\Users\Dhom\AppData\Local\Temp\cdt-user-data-dir3328865212396958053, disable-hang-monitor=true, disable-sync=true, disable-gpu=true}
8633 [main] DEBUG net.sf.jasperreports.chrome.ChromeInstanceRepository  - schedule chrome instance 1 idle timeout check after 900000
8635 [main] DEBUG net.sf.jasperreports.chrome.ChromeInstanceRepository  - schedule chrome instance 1 timeout after 7200000
8637 [main] DEBUG net.sf.jasperreports.chrome.ChromeInstance  - using chrome instance 1
9724 [main] DEBUG net.sf.jasperreports.chrome.StandardPageCreator  - created tab 902F248522DDE30E16DE2D3CD552FBD9
10020 [main] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Connecting to ws server ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10350 [Grizzly(1)] INFO com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Connected to ws ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10438 [main] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Sending message {"id":1,"method":"Log.enable","params":{}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10443 [Grizzly(2)] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Received message {"id":1,"result":{}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10462 [main] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Sending message {"id":2,"method":"Runtime.enable","params":{}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10466 [Grizzly(1)] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Received message {"method":"Runtime.executionContextCreated","params":{"context":{"id":1,"origin":"://","name":"","uniqueId":"-1037893551376530969.-1504717943394719259","auxData":{"isDefault":true,"type":"default","frameId":"902F248522DDE30E16DE2D3CD552FBD9"}}}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10467 [Grizzly(1)] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Received message {"id":2,"result":{}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10483 [main] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Sending message {"id":3,"method":"Page.enable","params":{}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10484 [Grizzly(2)] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Received message {"id":3,"result":{}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10486 [main] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Sending message {"id":4,"method":"Page.navigate","params":{"url":"file:/C:/Users/Dhom/AppData/Local/Temp/cv_1203404104433554074.html"}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10489 [Grizzly(1)] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Received message {"id":4,"result":{"frameId":"902F248522DDE30E16DE2D3CD552FBD9","loaderId":"82EC151841E6F98D209D3B011A2A2FBF"}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10490 [Grizzly(2)] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Received message {"method":"Page.frameStartedLoading","params":{"frameId":"902F248522DDE30E16DE2D3CD552FBD9"}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10494 [Grizzly(1)] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Received message {"method":"Runtime.executionContextsCleared","params":{}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10495 [Grizzly(1)] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Received message {"method":"Page.frameNavigated","params":{"frame":{"id":"902F248522DDE30E16DE2D3CD552FBD9","loaderId":"82EC151841E6F98D209D3B011A2A2FBF","url":"file:///C:/Users/Dhom/AppData/Local/Temp/cv_1203404104433554074.html","domainAndRegistry":"","securityOrigin":"file://","mimeType":"text/html","adFrameType":"none","secureContextType":"Secure","crossOriginIsolatedContextType":"NotIsolated","gatedAPIFeatures":["SharedArrayBuffers","SharedArrayBuffersTransferAllowed"]}}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10497 [Grizzly(2)] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Received message {"method":"Runtime.executionContextCreated","params":{"context":{"id":2,"origin":"file://","name":"","uniqueId":"-4551564477363548089.1093106506060346497","auxData":{"isDefault":true,"type":"default","frameId":"902F248522DDE30E16DE2D3CD552FBD9"}}}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10497 [main] DEBUG net.sf.jasperreports.chrome.BrowserService  - waiting for result, timeout 60000
10514 [Grizzly(1)] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Received message {"method":"Page.loadEventFired","params":{"timestamp":247828.898653}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10515 [Grizzly(1)] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Received message {"method":"Page.frameStoppedLoading","params":{"frameId":"902F248522DDE30E16DE2D3CD552FBD9"}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10516 [Grizzly(2)] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Received message {"method":"Page.domContentEventFired","params":{"timestamp":247828.898826}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10521 [pool-1-thread-1] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Sending message {"id":5,"method":"Runtime.evaluate","params":{"expression":"renderResult(true)"}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10522 [Grizzly(1)] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Received message {"id":5,"result":{"result":{"type":"object","subtype":"promise","className":"Promise","description":"Promise","objectId":"-3590625203254494070.2.1"}}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10547 [Grizzly(2)] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Received message {"method":"Runtime.consoleAPICalled","params":{"type":"log","args":[{"type":"string","value":"\ufffd"}],"executionContextId":2,"timestamp":1.621509394264413e+12,"stackTrace":{"callFrames":[{"functionName":"","scriptId":"6","url":"file:///C:/Users/Dhom/AppData/Local/Temp/jr_res_8667172309765663436_feeRenderer.min.js","lineNumber":4,"columnNumber":19892},{"functionName":"","scriptId":"4","url":"file:///C:/Users/Dhom/AppData/Local/Temp/jr_res_8570279741294855884_cv-component_static.js","lineNumber":47,"columnNumber":16},{"functionName":"execCb","scriptId":"3","url":"file:///C:/Users/Dhom/AppData/Local/Temp/jr_res_8609129502099932916_require.js","lineNumber":1695,"columnNumber":32},{"functionName":"check","scriptId":"3","url":"file:///C:/Users/Dhom/AppData/Local/Temp/jr_res_8609129502099932916_require.js","lineNumber":882,"columnNumber":50},{"functionName":"enable","scriptId":"3","url":"file:///C:/Users/Dhom/AppData/Local/Temp/jr_res_8609129502099932916_require.js","lineNumber":1175,"columnNumber":21},{"functionName":"init","scriptId":"3","url":"file:///C:/Users/Dhom/AppData/Local/Temp/jr_res_8609129502099932916_require.js","lineNumber":787,"columnNumber":25},{"functionName":"","scriptId":"3","url":"file:///C:/Users/Dhom/AppData/Local/Temp/jr_res_8609129502099932916_require.js","lineNumber":1459,"columnNumber":35}]}}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10552 [pool-1-thread-1] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Sending message {"id":6,"method":"Runtime.awaitPromise","params":{"returnByValue":true,"generatePreview":false,"promiseObjectId":"-3590625203254494070.2.1"}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10574 [Grizzly(1)] DEBUG com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Received message {"id":6,"result":{"result":{"type":"string","value":"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"><svg xmlns=\"http://www.w3.org/2000/svg\" id=\"element1445529659355478804511607219085981653206svg\" width=\"389\" height=\"240\" style=\"fill: transparent;\" version=\"1.1\"><defs><style type=\"text/css\">\nsvg circle { fill: rgb(240, 0, 255); }\n.fee-text { fill: white; font-family: arial; }</style></defs><text dominant-baseline=\"hanging\" aligment-baseline=\"hanging\" x=\"389\" y=\"0\" text-anchor=\"end\" class=\"fee-text\" style=\"font-size: 60px;\">.95</text><text dominant-baseline=\"hanging\" aligment-baseline=\"hanging\" x=\"305.578125\" y=\"0\" text-anchor=\"end\" class=\"fee-text\" style=\"font-size: 240px;\">8</text><text dominant-baseline=\"hanging\" aligment-baseline=\"hanging\" x=\"172.09375\" y=\"0\" text-anchor=\"end\" class=\"fee-text\" style=\"font-size: 120px;\">\ufffd</text></svg>"}}} on ws://localhost:1906/devtools/page/902F248522DDE30E16DE2D3CD552FBD9
10577 [main] INFO com.github.kklisura.cdt.services.impl.WebSocketServiceImpl  - Web socket connection closed NORMAL_CLOSURE,
10581 [pool-1-thread-1] DEBUG net.sf.jasperreports.chrome.BrowserService  - Page console LOG: ?

共有1个答案

叶德本
2023-03-14

这个问题最终导致Jasper Reports Library(直到版本6.17)中的一个bug,原因是没有正确地将Chrome和Jasper之间的通信流设置为使用UTF-8(请参见https://github.com/tibcosoftware/jasperreports/issues/189#issuecomment-845341796和html" target="_blank">https://github.com/tibcosoftware/jasperreports/issues/190)。结果,使用了JVM的默认字符编码,导致在使用某些字符时数据损坏。

解决方法1:找到原因的Jasper Reports维护者(github用户dadza)建议了一个解决方法,直到发布更正版本,包括在运行程序时通过添加以下选项来设置正确的字符编码:-dfile.encoding=utf-8

变通方法2:根据https://stackoverflow.com/a/14987992/15974273,在某些JVM版本中,可以在运行时使用dirty hack更改默认字符编码。虽然强烈建议不要使用它,但在主程序中添加以下代码行可能会使那些符号正确呈现(根据提供此解决方案的原始答案的一些注释,security manager可能要求关闭):

System.setProperty("file.encoding","UTF-8");
Field charset = Charset.class.getDeclaredField("defaultCharset");
charset.setAccessible(true);
charset.set(null,null);
 类似资料:
  • 问题内容: 我正在使用 JSF 2.1.8 实现Web应用程序,而 ui:include 标记存在问题。我有这个代码 该 navigationManagerSystem bean是JSF会话管理,在这一段代码首先呈现的,它应该是显示包括路径内容。如果我以这样的常量执行此操作,则包括这些工作,但是如果我将其放入变量中则不行,即使我已经看到变量在屏幕打印之前就保持了该值。我不知道它是否可以与包含/sy

  • 我为构建自己的CVC(一个基于Rapaehl地图的choropleth地图)而努力,但现在我在尝试发布我的项目时面临着两个大问题。 1)CVC似乎无法正常使用子数据集:可以选择报表的子数据集之一,但它无法识别字段。

  • 我正在编写一组定制的PrimeFaces组件,使用PrimeFaces5.0,并在JBoss EAP6.2中运行。 null 2.2在myFaceStest.taglib.xml中,我定义了输入标记: 2.3在input.java(我的自定义组件)中,我执行以下操作: 2.4此组件的呈现器包含以下内容: 给定这种设置,为什么自定义组件不会呈现?我的jboss日志中没有任何内容,即使日志级别设置为d

  • 我正在尝试一个用户界面3.5,所有组件都工作正常,除了selectonemenu,它不能正确渲染。 它的文本字段在选择任何下拉字段时不显示。 我不能发布图片,因为我没有那么多的声誉 代码 屏幕拍摄 我怎样才能解决这个问题?

  • 问题内容: 我已经尝试了两个类组件: 并使用功能组件: 页面上显示的x值永远不会改变,或者似乎是随机改变的。是什么赋予了? 问题答案: 当您告诉React某些更改时,React仅知道使用其为状态管理提供的功能来重新渲染: 另外,功能组件应该是 纯 组件(没有副作用),因此要更新它们,React会给我们带来麻烦: 因此,您不能只是分配给一个变量并期望React知道:您必须使用它的更新函数,以便它知道

  • 我正在开发一个使用Xamarin的自定义键盘。 我的键盘视图对视图容器本身及其子键视图都有一个覆盖的OnDraw()。我还为每个视图适当地使用SetWillNotDraw(false)。它目前在我的Nexus 10平板电脑上的5.0.1中运行良好。 在Android 6.0.1中,在Nexus 6和Nexus 6P上,键盘视图可以正确地绘制自身(只是背景颜色)。然而,即使我遍历视图层次结构并强制对