PDF不支持日文字符和打印?

时间:2017-04-06 04:28:50

标签: java jasper-reports export-to-pdf

我使用的是jasper 0.6.0版本和itext 1.0.1。我正在使用MS Mincho jar文件和itextasian以及itextasiancmaps jar文件。它是打印日本characetrs ???用PDF格式

后来我将Jasper版本升级到6.0.0,将itext版本升级到5.50仍然打印为???

我已经附加了Java源代码和jrxml。我的jrxml有问题,还是我需要一些其他的jar来支持PDF上的这些字符

    <?xml version="1.0" encoding="UTF-8"?>
    // for 0.6.0 version Doctype
    <!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "http://www.jasperreports.com/dtds/jasperreport.dtd">
    // for 6.0.0 version Doctype
    <!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

    <jasperReport name="Roster" columnCount="1" printOrder="Vertical" pageWidth="635" pageHeight="842" columnWidth="270" columnSpacing="15" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30" isTitleNewPage="false" isSummaryNewPage="false">
    <reportFont name="Arial_Normal" isDefault="true" fontName="Arial" size="10" pdfFontName="Helvetica" pdfEncoding="UTF-8" isPdfEmbedded="true"/>
    <reportFont name="Arial_Bold" isDefault="true" fontName="Arial" size="10" pdfFontName="Helvetica" pdfEncoding="UTF-8" isPdfEmbedded="true" isBold="true"/>
    <parameter name="TestDate" class="java.lang.String"/>
    <parameter name="TestCenter" class="java.lang.String"/>
    <parameter name="Session" class="java.lang.String"/>
    <parameter name="SessionId" class="java.lang.Integer"/>
    <parameter name="ProctorName" class="java.lang.String"/>
    <field name="ID" class="java.lang.String"/>
    <field name="CandidateName" class="java.lang.String"/>
    <field name="TestPortion" class="java.lang.String"/>
    <field name="SeatNo" class="java.lang.String"/>
    <field name="TestForm" class="java.lang.String"/>
    <field name="PaperAndPencil" class="java.lang.String"/>
    <field name="ExtendedTime" class="java.lang.Integer"/>
    <field name="CreditCardNumberString" class="java.lang.String"/>
    <field name="AmountPaidAndDateOfPaymentString" class="java.lang.String"/>
    <field name="AllCreditsFinalMessageString" class="java.lang.String"/>
    <field name="IAgreeToPayMessageString" class="java.lang.String"/>
    <field name="SessionId" class="java.lang.Integer"/>
    <variable name="ExamPortionsStr" class="java.lang.String" resetType="None" calculation="Nothing">
        <variableExpression>"Exam Portion(s)"</variableExpression>
        <initialValueExpression>"Exam Portion(s)"      </initialValueExpression>
    </variable>

    <group name="Candidate" minHeightToStartNewPage="200">
        <groupExpression>121</groupExpression>
        <groupHeader>
            <band/>
        </groupHeader>
        <groupFooter>
            <band height="125">
                <line>
                    <reportElement x="0" y="110" width="595" height="0" forecolor="#999999" positionType="Float"/>
                    <graphicElement stretchType="NoStretch" pen="Thin"/>
                </line>
            </band>
        </groupFooter>
    </group>
    <title>
        <band height="43">
            <staticText>
                <reportElement x="0" y="0" width="595" height="23"/>
                <textElement textAlignment="Center">
                    <font reportFont="Arial_Bold" size="15"/>
                </textElement>
                <text>PSI EXAMINATION SERVICES</text>
            </staticText>
            <staticText>
                <reportElement x="0" y="25" width="595" height="15"/>
                <textElement textAlignment="Center">
                    <font reportFont="Arial_Normal" size="10"/>
                </textElement>
                <text>Examination Roster</text>
            </staticText>
            <line>
                <reportElement x="0" y="40" width="595" height="0" forecolor="#000000"/>
                <graphicElement stretchType="NoStretch"/>
            </line>
        </band>
    </title>
    <pageHeader>
        <band height="40">
            <textField>
                <reportElement x="0" y="0" width="200" height="12"/>
                <textElement textAlignment="Left">
                    <font size="10"/>
                </textElement>
                <textFieldExpression class="java.lang.String">"Test Date : 05/04/2016" </textFieldExpression>
            </textField>
            <textField>
                <reportElement x="390" y="0" width="200" height="12"/>
                <textElement textAlignment="Right">
                    <font size="10"/>
                </textElement>
                <textFieldExpression class="java.lang.String">"Proctor : Ganesh "</textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="17" width="200" height="12"/>
                <textElement textAlignment="Left">
                    <font size="10"/>
                </textElement>
                <textFieldExpression class="java.lang.String">"Session : Morning "</textFieldExpression>
            </textField>
            <textField>
                <reportElement x="200" y="17" width="200" height="12"/>
                <textElement textAlignment="Left">
                    <font size="10"/>
                </textElement>
                <textFieldExpression class="java.lang.String">"Session # : 1 " </textFieldExpression>
            </textField>            
            <textField>
                <reportElement x="390" y="17" width="200" height="12"/>
                <textElement textAlignment="Right">
                    <font size="10"/>
                </textElement>
                <textFieldExpression class="java.lang.String">"Test Center : Akron " </textFieldExpression>
            </textField>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="20">
            <rectangle>
                <reportElement x="0" y="0" width="595" height="15" backcolor="#555555"/>
                <graphicElement stretchType="NoStretch"/>
            </rectangle>
            <staticText>
                <reportElement x="0" y="2" width="50" height="15" forecolor="#FFFFFF"/>
                <textElement textAlignment="Center">
                    <font reportFont="Arial_Normal" size="10"/>
                </textElement>
                <text>ID</text>
            </staticText>
            <staticText>
                <reportElement x="55" y="0" width="100" height="15" forecolor="#FFFFFF"/>
                <textElement textAlignment="Center">
                    <font reportFont="Arial_Normal" size="10"/>
                </textElement>
                <text>Candidate Name</text>
            </staticText>
            <staticText>
                <reportElement x="325" y="0" width="50" height="15" forecolor="#FFFFFF"/>
                <textElement textAlignment="Center">
                    <font reportFont="Arial_Normal" size="10"/>
                </textElement>
                <text>Seat</text>
            </staticText>
            <staticText>
                <reportElement x="380" y="0" width="50" height="15" forecolor="#FFFFFF"/>
                <textElement textAlignment="Center">
                    <font reportFont="Arial_Normal" size="10"/>
                </textElement>
                <text>Test Form</text>
            </staticText>
            <staticText>
                <reportElement x="435" y="0" width="70" height="15" forecolor="#FFFFFF"/>
                <textElement textAlignment="Center">
                    <font reportFont="Arial_Normal" size="10"/>
                </textElement>
                <text>Test Mode</text>
            </staticText>
            <staticText>
                <reportElement x="510" y="0" width="85" height="15" forecolor="#FFFFFF"/>
                <textElement textAlignment="Left">
                    <font reportFont="Arial_Normal" size="10"/>
                </textElement>
                <text>Extended Time</text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="13">
            <textField isStretchWithOverflow="true">
                <reportElement x="0" y="0" width="65" height="10"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression class="java.lang.String">
                   <![CDATA["1"]]>
                </textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement x="70" y="0" width="260" height="10" />
                <textElement textAlignment="Left">
                    <font fontName="MS Mincho"/>
                </textElement>
                <textFieldExpression class="java.lang.String">
                    <![CDATA["ガネーシュ"]]>
                </textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement x="0" y="12" width="90" height="10" positionType="Float"/>
                <textElement textAlignment="Left">
                    <font reportFont="Arial_Bold"/>
                </textElement>
                <textFieldExpression class="java.lang.String">
                   <![CDATA["Nothing"]]>
                </textFieldExpression>
            </textField>          
            <textField isStretchWithOverflow="true">
                <reportElement x="110" y="12" width="485" height="10" positionType="Float"/>
                <textElement textAlignment="Left">
                    <font reportFont="Arial_Bold"/>
                </textElement>
                <textFieldExpression class="java.lang.String">
                    <![CDATA["Nothing"]]>
                </textFieldExpression>
            </textField>
            <textField isBlankWhenNull="true" isStretchWithOverflow="true">
                <reportElement x="325" y="0" width="50" height="10"/>
                <textElement textAlignment="Left">
                    <font reportFont="Arial_Bold"/>
                </textElement>
                <textFieldExpression class="java.lang.String">
                    <![CDATA["10"]]>
                </textFieldExpression>
            </textField>
            <textField isBlankWhenNull="true" isStretchWithOverflow="true">
                <reportElement x="380" y="0" width="50" height="10"/>
                <textElement textAlignment="Center">
                    <font reportFont="Arial_Bold"/>
                </textElement>
                <textFieldExpression class="java.lang.String">
                    <![CDATA["Good"]]>
                </textFieldExpression>
            </textField>
            <textField isBlankWhenNull="true" isStretchWithOverflow="true">
                <reportElement x="435" y="0" width="70" height="10"/>
                <textElement textAlignment="Center">
                    <font reportFont="Arial_Bold"/>
                </textElement>
                <textFieldExpression class="java.lang.String">
                    <![CDATA["Nothing"]]>
                </textFieldExpression>
            </textField>
            <textField isBlankWhenNull="true" isStretchWithOverflow="true">
                <reportElement x="510" y="0" width="85" height="10"/>
                <textElement textAlignment="Left"/>
                <textFieldExpression class="java.lang.Integer">
                   <![CDATA[10]]>
                </textFieldExpression>
            </textField>
            <textField isBlankWhenNull="true" isStretchWithOverflow="true">
                <reportElement x="0" y="50" width="180" height="10"/>
                <textElement textAlignment="Left"/>
                <textFieldExpression class="java.lang.String">
                    <![CDATA["Nothing"]]>
                </textFieldExpression>
            </textField>
            <textField isBlankWhenNull="true" isStretchWithOverflow="true">
                <reportElement x="0" y="65" width="300" height="10"/>
                <textElement textAlignment="Left"/>
                <textFieldExpression class="java.lang.String">
                    <![CDATA["Nothing"]]>
                </textFieldExpression>
            </textField>
            <textField isBlankWhenNull="true" isStretchWithOverflow="true">
                <reportElement x="0" y="80" width="300" height="10"/>
                <textElement textAlignment="Left"/>
                <textFieldExpression class="java.lang.String">
                    <![CDATA["Nothing"]]>
                </textFieldExpression>
            </textField>
            <textField isBlankWhenNull="true" isStretchWithOverflow="true">
                <reportElement x="0" y="95" width="400" height="10"/>
                <textElement textAlignment="Left"/>
                <textFieldExpression class="java.lang.String">
                    <![CDATA["Nothing"]]>
                </textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="350" y="35" width="250" height="12"/>
                <textElement textAlignment="Right">
                    <font reportFont="Arial_Normal" size="9"/>
                </textElement>
                <text>Examinee Signature:________________________</text>
            </staticText>            
            <staticText>
                <reportElement x="350" y="65" width="250" height="12"/>
                <textElement textAlignment="Right">
                    <font reportFont="Arial_Normal" size="9"/>
                </textElement>
                <text>Narrative/Fact Sheet:________________________</text>
            </staticText>
            <staticText>
                <reportElement x="350" y="95" width="250" height="12"/>
                <textElement textAlignment="Right">
                    <font reportFont="Arial_Normal" size="9"/>
                </textElement>
                <text>Proctor Verification:________________________</text>
            </staticText>            
    </band>
    </detail>
    <columnFooter>
        <band/>
    </columnFooter>
    <pageFooter>
        <band/>
    </pageFooter>
    <summary>
        <band/>
    </summary>
</jasperReport>

和Java代码,

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;


public class PdfFromXmlFile {
  public static void main(String[] args) throws JRException, IOException {
     JasperReport jasperReport =JasperCompileManager.compileReport("C:\\Users\\gbhagavath\\Downloads\\StyledTextReport/StyledTextReport2.jrxml");
   // Parameters for report
   Map<String, Object> parameters = new HashMap<String, Object>();

   // DataSource
   // This is simple example, no database.
   // then using empty datasource.
   JRDataSource dataSource = new JREmptyDataSource();

   JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,parameters, dataSource);

   // Make sure the output directory exists.
   File outDir = new File("C:/jasperoutput");
   outDir.mkdirs();

   // Export to PDF.
   JasperExportManager.exportReportToPdfFile(jasperPrint,"C:/jasperoutput/StyledTextReport.pdf");

   System.out.println("Done!");
   }
  }

我失踪的地方?或者我需要做些什么来解决这个问题

 <?xml version="1.0" encoding="UTF-8"?>
    <!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
    <!-- 2017-04-04T14:33:09 -->
    <!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
    <jasperReport name="StyledTextReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30">
    <title>
        <band height="640">
            <staticText>
                <reportElement x="14" y="63" width="521" height="24"/>
                <textElement>
                    <font fontName="MS Mincho" size="15"/>
                </textElement>
                <text><![CDATA[Cyrillic: 芒果]]></text>
            </staticText>
            <textField>
                <reportElement x="0" y="150" width="555" height="60" />
                <textElement textAlignment="Center">
                    <font size="16"/>
                </textElement>
                <textFieldExpression><![CDATA["This is a text field element that contains styled text displaying the text-only version of the <style size=\"12\" isBold=\"true\" forecolor=\"black\" pdfFontName=\"Helvetica-Bold\">^<style forecolor=\"#808080\">Jasper</style><style forecolor=\"#990000\">Reports</style></style> logo."]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement mode="Opaque" x="0" y="220" width="270" height="320" backcolor="#CCFFCC" />
                <textElement>
                    <font size="16"/>
                </textElement>
                <text><![CDATA[Here is the list with the attributes supported by the <style isBold="true" pdfFontName="Helvetica-Bold">style</style> tag used to format the text content inside a styled text element:

           fontName
       <style size="32">size</style>
       <style isBold="true" pdfFontName="Helvetica-Bold">isBold</style>
       <style isItalic="true" pdfFontName="Helvetica-Oblique">isItalic</style>
       <style isUnderline="true">isUnderline</style>
       <style isStrikeThrough="true">isStrikeThrough</style>
       <style pdfFontName="Helvetica-Bold">Helvetica-Bold</style>
       <style pdfEncoding="UTF-8">芒果</style>
       <style isPdfEmbedded="false">false</style>

     <style forecolor="red">forecolor</style>
     <style backcolor="blue" forecolor="white" isBold="true" pdfFontName="Helvetica-Bold" pdfEncoding="UTF-8" isPdfEmbedded="false">backcolor</style>]]></text>
            </staticText>
            <staticText>
                <reportElement mode="Opaque" x="280" y="220" width="270" height="320" backcolor="#CCFFCC"/>
                <textElement>
                    <font fontName="MS Mincho" size="16"/>
                </textElement>
                <text><![CDATA[Here is the list with the supported HTML tags 芒果  : 

       <font size="12">&amp;lt;font size=&amp;quot;12&amp;quot;&amp;gt;</font>
       <font color="red">&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;</font>
       <font face="Comic Sans MS">&amp;lt;font face=&amp;quot;Comic Sans MS&amp;quot;&amp;gt;</font>
       <b>bold</b>
        <i>italic</i>
      <u>underlined</u>
       an empty line follows(&amp;lt;br/&amp;gt;):
         <br/>
        A bulleted list:
        <li>item 1</li>
        ]]></text>
            </staticText>
            <textField>
                <reportElement x="0" y="550" width="555" height="20" />
                <textElement textAlignment="Center">
                    <font size="16"/>
                </textElement>
                <textFieldExpression><![CDATA["ampersand &amp;amp; less &amp;lt; greater &amp;gt; quote &amp;quot;"]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="0" y="580" width="555" height="20" />
                <textElement textAlignment="Center" >
                    <font size="16"/>
                </textElement>
                <text><![CDATA[ampersand &amp;amp; less &amp;lt; greater &amp;gt; quote &amp;quot;]]></text>
            </staticText>
            <textField isStretchWithOverflow="true">
                <reportElement mode="Opaque" x="0" y="610" width="555" height="20" forecolor="#0000FF" backcolor="#FFFF00"/>
                <textElement textAlignment="Center" />
                <textFieldExpression><![CDATA["<style size=\"16\">1</style>\n<style size=\"24\">2</style>\n<style size=\"32\">3</style>\n<style size=\"24\">2</style>\n<style size=\"16\">1</style>\n"]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

0 个答案:

没有答案