gpt4 book ai didi

jakarta-ee - 导出 PDF 中的 Primefaces 图表

转载 作者:行者123 更新时间:2023-12-03 17:02:44 25 4
gpt4 key购买 nike

首先,我必须告诉你,我的母语不是英语,所以我希望我能正确地向你解释我的问题。

我在以 PDF 格式导出条形图时遇到了一些问题。我正在使用 PrimeFaces 3.5 和 Tomcat v7。

经过一番研究,我了解到无法直接导出 PDF 格式的图表。但它似乎是某种方式将图表保存为 png 或 jpeg 之类的图像。

我发现了这个:http://www.primefaces.org/showcase-labs/ui/chartExport.jsf但这只是一种打印屏幕方式,我想保存它以便在我的 pdf 报告中使用它。

我也看到了 JFreeChart 解决方案,但我真的想在我的 html 页面和我的 PDF 报告中保留相同的图表。

这是我的代码:

<h:form id="finalreport">
<c:if test="#{treeBean.finalPrintReport.create == 1}">
<h1>
<h:outputText value="#{treeBean.finalPrintReport.namefinalreport}"
escape="false" />
</h1>
<p:dataTable id="dataTableReport" var="row"
value="#{treeBean.finalPrintReport.allData}" paginator="true"
rows="10">
<p:columns value="#{treeBean.finalPrintReport.column}" var="column"
columnIndexVar="colIndex">
<f:facet name="header">
<h:outputText value="#{column}" />
</f:facet>

<h:outputText value="#{row[colIndex]}" />
</p:columns>
</p:dataTable>
<br />

<h:commandLink>
<p:graphicImage value="/images/pdf.png" />
<p:dataExporter type="pdf" target="dataTableReport"
fileName="Report" preProcessor="#{treeBean.createPDF}" />
</h:commandLink>
</c:if>
</h:form>
<!-- Graph -->
<h:form id="graph">
<c:if test="#{treeBean.finalPrintReport.create == 1}">
<c:if test="#{treeBean.finalPrintReport.propertyOfFinalReport.graph == true}">
<p:barChart id="basic" value="#{treeBean.chartbar2d}"
legendPosition="ne" title="Basic Bar Chart"
style="height:300px" />
</c:if>
</c:if>
</h:form>

谢谢

最佳答案

我不知道这是否是解决此问题的最佳方法,但我通常会获取从图表导出的图像的 src 属性,然后将其发送回托管 Bean。您可以在那里创建一个新图像并使用您喜欢的方式导出。

例子:

在你的 MB 中

/** String Base64 that represents the image bytes */
private String chartImageSrcBase64;

页面

<h:inputHidden id="chartImageSrc" 
value="#{myMB.chartImageSrcBase64}" />

<p:remoteCommand name="exportToPdfRemoteCommand"
action="#{myMB.exportPdf}">

<p:commandButton type="button" 
value="Export to PDF"
onclick="exportToPdf()"/>

exportToPdf() 是您页面中的自定义 JS 函数

function exportToPdf() {
//get your image
var imageElement = PF('chart').exportAsImage();

//get the value of the 'src' attribute of this object and fill the hidden input
$("#yourForm\\:chartImageSrc").val($(imageElement).attr("src"));

//send the image (converted in text) to your MB
exportToPdfRemoteCommand();
}

进入服务器后,您可以将基于文本的图像再次转换为真实图像,并使用您喜欢的方式构建 PDF 文档。

关于jakarta-ee - 导出 PDF 中的 Primefaces 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18125011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com