Package net.sf.jasperreports.pdf
Class JRPdfExporter
- java.lang.Object
-
- net.sf.jasperreports.engine.JRAbstractExporter<PdfReportConfiguration,PdfExporterConfiguration,OutputStreamExporterOutput,JRPdfExporterContext>
-
- net.sf.jasperreports.pdf.JRPdfExporter
-
- All Implemented Interfaces:
Exporter<ExporterInput,PdfReportConfiguration,PdfExporterConfiguration,OutputStreamExporterOutput>
public class JRPdfExporter extends JRAbstractExporter<PdfReportConfiguration,PdfExporterConfiguration,OutputStreamExporterOutput,JRPdfExporterContext>
Exports a JasperReports document to PDF format. It has binary output type and exports the document to a free-form layout. As its name indicates, PDF is a very precise and complex document format that ensures documents will look and print the same on all platforms. This is why the PDF exporter implemented by theJRPdfExporter
class in JasperReports is one of the best exporters. The output it produces is almost of the same quality as that produced by theJRGraphics2DExporter
, which is always the reference. TheJRPdfExporter
implementation uses OpenPDF, which is a specialized PDF-generating library. PDF is a binary document format that allows absolute positioning of the elements inside a page, so the existing PDF exporter does not have the limitations of a grid exporter. It also works very well in batch mode because it allows concatenation of multiple documents within the same PDF file, even if the files have different page sizes.Font Mappings
Exporting to PDF requires mapping the fonts using three attributes:pdfFontName
,pdfEncoding
andisPdfEmbedded
. Even though these three attributes are still supported in JRXML and the API, we recommend making the PDF font mappings at export time using font extensions. When exporting documents to PDF, for each combination of the threefontName
,isBold
, andisItalic
font attributes, there must be an equivalent combination of the PDF-related font attributespdfFontName
,pdfEncoding
andisPdfEmbedded
. Equivalent combination means one that causes the text elements to be rendered exactly the same (or at least as closely as possible) in PDF and the built-in Graphics2D exporter, which is the reference. In some cases, there is no font file available to use with the pdfFontName attribute in order to render bold and italic text exactly like the Graphics2D exporter renders it in AWT. Those fonts might only have a normal style variant and no variants for bold and italic. In such cases, the PDF exporter (the OpenPDF library, to be more precise) is able to simulate those styles by applying transformations to the normal font glyphs. TheJRPdfExporter
internally acquires the needed PDF font based on the font extension mechanism (see thegetFont(Map, Locale, boolean)
method.Batch Mode Bookmarks
When several JasperPrint documents must be concatenated in the same PDF file by batch export, one can introduce PDF bookmarks in the resulting PDF document to mark the beginning of each individual document that was part of the initial document list. These bookmarks have the same name as the original JasperPrint document as specified by thejasperPrint.getName()
property. However, users can turn on and off the creation of those bookmarks by turning on or off theisCreatingBatchModeBookmarks()
exporter configuration setting. The exporter does not create such bookmarks by default.Encrypted PDF
In some cases, users might want to encrypt the PDF documents generated by JasperReports so that only authorized viewers can have access to those documents. There are five exporter configuration settings for this (seePdfExporterConfiguration
):PDF Version and Compression
Some applications require marking the generated files with a particular PDF specifications version. Related export configuration settings are the following (seePdfExporterConfiguration
): Since version 1.5, the PDF format supports compression. By default, the PDF exporter in JasperReports does not create compressed PDF documents, but this feature can be turned on using theisCompressed()
exporter configuration setting. Note that because compressed PDFs are available only since PDF version 1.5, the PDF version of the resulting document is set to 1.5 automatically if compression is turned on.Word Wrap and Line Break Policy
By default, the PDF exporter does not guarantee that text with the same style properties will be rendered exactly as it is using AWT. The word wrap and line break policy is slightly different, and in some cases it might cause portions of text to disappear at the end of longer text paragraphs. To make sure this does not happen, one can configure the PDF exporter to use the AWT word wrap and line break policy by setting theisForceLineBreakPolicy()
exporter configuration setting to true. Note that this feature is not turned on by default, because it affects the exporter performance. This default behavior that applies in the absence of the mentioned export parameter can be controlled using thenet.sf.jasperreports.export.pdf.force.linebreak.policy
configuration propertyJavaScript Actions
The PDF specifications provide a means for the automation of various processes, such as the automatic printing of the document when it is opened. PDF viewer applications are able to execute Acrobat JavaScript code that is embedded in the PDF and associated with different events. JasperReports only allows inserting Acrobat JavaScript code. This code gets executed when the PDF document is opened in the viewer. This can be achieved using thegetPdfJavaScript()
configuration setting, which retrieve the Acrobat JavaScript source code. Note that Acrobat JavaScript is a programming language based on JavaScript. More details about this can be found in the OpenPDF documentation.Metadata Information
PDF documents can store metadata information such as the author of the document, its title, and keywords. JasperReports exposes this feature of PDF through special exporter configuration settings available in thePdfExporterConfiguration
class. They are all listed following:getMetadataAuthor()
getMetadataCreator()
getMetadataKeywords()
getMetadataSubject()
getMetadataTitle()
Rendering SVG Using Shapes
TheisForceSvgShapes()
flag is used to force the rendering of SVG images using shapes on the PDFGraphics2D
context. This allows fonts to be rendered as shapes, thus avoiding any font mapping issues that might cause Unicode text to not show up properly; however, it has the disadvantage of producing larger PDF files. By default, the flag is set to true, mainly due to backward-compatibility reasons. To reduce PDF file size for documents containing SVG images such as charts, this flag should be set to false. However, in such a case, the accuracy of the text content rendered by the SVG element in PDF depends on the correct PDF font information being available in the SVG implementation itself. In JasperReports, SVG elements are rendered usingRenderable
implementations, which are most likely subclasses of theAbstractRenderToImageDataRenderer
class. SVG renderer implementations should be concerned only with implementing thepublic void render(JasperReportsContext jasperReportsContext, Graphics2D grx, Rectangle2D rectangle)
method, which should contain all the code required for rendering the SVG on a Graphics2D context. Correct PDF font information means that thejava.awt.Font
objects used to draw text on theGraphics2D
context should have PDF-related text attributes embedded so that when rendered on a PDFGraphics2D
context, the exporter can make use of them. Embedding PDF-related text attributes into the SVG means using the following text attributes when creatingjava.awt.Font
to render text in the SVG renderer implementation: The built-in chart component in JasperReports hides this complexity of dealing with fonts in a SVG renderer by exposing to the end user the usual three PDF-specific font attributes (pdfFontName
,pdfEncoding
, andisPdfEmbedded
) to be set along with the normal font attributes every time a font setting is made for the chart title, subtitle, chart legend, or axis. This feature can be controlled system-wide using thenet.sf.jasperreports.export.pdf.force.svg.shapes
configuration property. TheisForceSvgShapes()
export configuration setting overrides the configuration property value, if present.Section 508 Compliance
PDF files can contain hidden tags that describe the structure of the document. Some of the tags are used by the automated reader tool that reads PDF documents aloud to people with disabilities. The PDF tags feature of JasperReports allows adding hidden PDF tags to the files generated by the JasperReports PDF exporter. The resulting files comply with the requirements of the Section 508 of the U.S. Rehabilitation Act (http://www.section508.gov/).Producing Tagged PDF Files
By default, the JasperReports exporter does not put any hidden structural tags inside its generated PDF files. In order to turn on the creation of hidden structural tags, any of the following can be used:- setting to true the
isTagged()
configuration flag - setting to true the
net.sf.jasperreports.export.pdf.tagged
configuration property.
Setting the PDF File Language
When a full accessibility check is requested from Acrobat Professional, among the things it determines is whether the PDF file or the various pieces of content inside it have a language set. JasperReports allows setting the language for the entire content by doing any one of the following:- using the
getTagLanguage()
configuration setting to retrieve the language as ajava.lang.String
value; - using the
net.sf.jasperreports.export.pdf.tag.language
configuration property globally or at report level
Alternate Text for Images
In tagged PDF files, image elements can be described in alternate text that is read by the automated reader. The text is specified using thehyperlinkTooltipExpression
property of the image element in JRXML. For more information about tagged PDF documents in JasperReports, please consult theJRPdfExporterTagHelper
class.- Author:
- Teodor Danciu (teodord@users.sourceforge.net)
- See Also:
PdfExporterConfiguration
,PdfReportConfiguration
,JRTextAttribute.IS_PDF_EMBEDDED
,JRTextAttribute.PDF_ENCODING
,JRTextAttribute.PDF_FONT_NAME
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
JRPdfExporter.Bookmark
protected static class
JRPdfExporter.BookmarkStack
protected class
JRPdfExporter.ExporterContext
-
Nested classes/interfaces inherited from class net.sf.jasperreports.engine.JRAbstractExporter
JRAbstractExporter.BaseExporterContext, JRAbstractExporter.PageRange
-
-
Field Summary
-
Fields inherited from class net.sf.jasperreports.engine.JRAbstractExporter
allSelector, crtCompositeConfiguration, crtCompositeItemConfiguration, crtItem, dateFormatCache, EXCEPTION_MESSAGE_KEY_EMPTY_INPUT_SOURCE_IN_BATCH_MODE, EXCEPTION_MESSAGE_KEY_END_PAGE_INDEX_OUT_OF_RANGE, EXCEPTION_MESSAGE_KEY_INVALID_IMAGE_NAME, EXCEPTION_MESSAGE_KEY_INVALID_ZOOM_RATIO, EXCEPTION_MESSAGE_KEY_NO_INPUT_SOURCE, EXCEPTION_MESSAGE_KEY_NO_OUTPUT_SPECIFIED, EXCEPTION_MESSAGE_KEY_OUTPUT_WRITER_ERROR, EXCEPTION_MESSAGE_KEY_PAGE_INDEX_OUT_OF_RANGE, EXCEPTION_MESSAGE_KEY_START_PAGE_INDEX_OUT_OF_RANGE, exporterConfiguration, exporterContext, exporterInput, exporterOutput, filter, fontUtil, hyperlinkProducerFactory, itemConfiguration, jasperPrint, jasperReportsContext, noBackcolorSelector, noneSelector, numberFormatCache, propertiesUtil, PROPERTY_DEFAULT_FILTER_FACTORY, PROPERTY_SUFFIX_DEFAULT_FILTER_FACTORY, rendererUtil, styledTextUtil, textValueClasses
-
-
Constructor Summary
Constructors Constructor Description JRPdfExporter()
JRPdfExporter(JasperReportsContext jasperReportsContext)
-
Method Summary
-
Methods inherited from class net.sf.jasperreports.engine.JRAbstractExporter
checkInterrupted, createFilter, defaultParseNumber, ensureInput, ensureJasperReportsContext, ensureOutput, getBooleanCellValue, getCurrentConfiguration, getCurrentItemConfiguration, getCurrentJasperPrint, getDateCellValue, getDateFormat, getExporterConfiguration, getExporterContext, getExporterInput, getExporterOutput, getHyperlinkProducer, getJasperReportsContext, getLocale, getNumberCellValue, getNumberFormat, getPageRange, getPropertiesUtil, getRendererUtil, getReportContext, getReportExportConfiguration, getRepository, getStyledText, getStyledText, getTextFormatFactoryClass, getTextTimeZone, getTextValue, getTextValueString, insideFrame, reset, resetExportContext, restoreElementOffsets, setConfiguration, setConfiguration, setCurrentExporterInputItem, setExporterInput, setExporterOutput, setFrameElementsOffset, setJasperReportsContext, setOffset, setOffset, setReportContext
-
-
-
-
Field Detail
-
PDF_EXPORTER_PROPERTIES_PREFIX
public static final String PDF_EXPORTER_PROPERTIES_PREFIX
- See Also:
- Constant Field Values
-
EXCEPTION_MESSAGE_KEY_DOCUMENT_ERROR
public static final String EXCEPTION_MESSAGE_KEY_DOCUMENT_ERROR
- See Also:
- Constant Field Values
-
EXCEPTION_MESSAGE_KEY_FONT_LOADING_ERROR
public static final String EXCEPTION_MESSAGE_KEY_FONT_LOADING_ERROR
- See Also:
- Constant Field Values
-
EXCEPTION_MESSAGE_KEY_REPORT_GENERATION_ERROR
public static final String EXCEPTION_MESSAGE_KEY_REPORT_GENERATION_ERROR
- See Also:
- Constant Field Values
-
PDF_FONT_FILES_PREFIX
public static final String PDF_FONT_FILES_PREFIX
Prefix of properties that specify font files for the PDF exporter.- See Also:
- Constant Field Values
-
PDF_FONT_DIRS_PREFIX
public static final String PDF_FONT_DIRS_PREFIX
Prefix of properties that specify font directories for the PDF exporter.- See Also:
- Constant Field Values
-
PDF_FIELD_TYPE
public static final String PDF_FIELD_TYPE
- See Also:
- Constant Field Values
-
PDF_FIELD_TEXT_MULTILINE
public static final String PDF_FIELD_TEXT_MULTILINE
- See Also:
- Constant Field Values
-
PDF_FIELD_VALUE
public static final String PDF_FIELD_VALUE
- See Also:
- Constant Field Values
-
PDF_FIELD_CHECK_TYPE
public static final String PDF_FIELD_CHECK_TYPE
- See Also:
- Constant Field Values
-
PDF_FIELD_NAME
public static final String PDF_FIELD_NAME
- See Also:
- Constant Field Values
-
PDF_FIELD_CHECKED
public static final String PDF_FIELD_CHECKED
- See Also:
- Constant Field Values
-
PDF_FIELD_READ_ONLY
public static final String PDF_FIELD_READ_ONLY
- See Also:
- Constant Field Values
-
PDF_FIELD_BORDER_STYLE
public static final String PDF_FIELD_BORDER_STYLE
- See Also:
- Constant Field Values
-
PDF_FIELD_CHOICE_SEPARATORS
public static final String PDF_FIELD_CHOICE_SEPARATORS
- See Also:
- Constant Field Values
-
PDF_FIELD_CHOICES
public static final String PDF_FIELD_CHOICES
- See Also:
- Constant Field Values
-
PDF_FIELD_COMBO_EDIT
public static final String PDF_FIELD_COMBO_EDIT
- See Also:
- Constant Field Values
-
LEGACY_TARGET_BLANK_LINKS
public static final String LEGACY_TARGET_BLANK_LINKS
- See Also:
- Constant Field Values
-
LEGACY_TEXT_MEASURING_FIX
public static final String LEGACY_TEXT_MEASURING_FIX
- See Also:
- Constant Field Values
-
PDF_EXPORTER_KEY
public static final String PDF_EXPORTER_KEY
The exporter key, as used inGenericElementHandlerEnviroment.getElementHandler(JRGenericElementType, String)
.- See Also:
- Constant Field Values
-
PDF_PRODUCER_FACTORY_PROPERTY
public static final String PDF_PRODUCER_FACTORY_PROPERTY
- See Also:
- Constant Field Values
-
JR_PAGE_ANCHOR_PREFIX
protected static final String JR_PAGE_ANCHOR_PREFIX
- See Also:
- Constant Field Values
-
fontsRegistered
protected static boolean fontsRegistered
-
pdfProducer
protected PdfProducer pdfProducer
-
pdfContent
protected PdfContent pdfContent
-
tagHelper
protected JRPdfExporterTagHelper tagHelper
-
cmykColorSpace
protected ColorSpace cmykColorSpace
-
reportIndex
protected int reportIndex
-
pageFormat
protected PrintPageFormat pageFormat
-
crtDocumentPageNumber
protected int crtDocumentPageNumber
-
permissions
protected int permissions
-
renderersCache
protected RenderersCache renderersCache
-
loadedImagesMap
protected Map<String,Pair<PdfImage,ExifOrientationEnum>> loadedImagesMap
-
-
Constructor Detail
-
JRPdfExporter
public JRPdfExporter()
- See Also:
JRPdfExporter(JasperReportsContext)
-
JRPdfExporter
public JRPdfExporter(JasperReportsContext jasperReportsContext)
-
-
Method Detail
-
getConfigurationInterface
protected Class<PdfExporterConfiguration> getConfigurationInterface()
-
getItemConfigurationInterface
protected Class<PdfReportConfiguration> getItemConfigurationInterface()
-
exportReport
public void exportReport() throws JRException
Description copied from interface:Exporter
Performs the export.- Specified by:
exportReport
in interfaceExporter<ExporterInput,PdfReportConfiguration,PdfExporterConfiguration,OutputStreamExporterOutput>
- Specified by:
exportReport
in classJRAbstractExporter<PdfReportConfiguration,PdfExporterConfiguration,OutputStreamExporterOutput,JRPdfExporterContext>
- Throws:
JRException
-
initExport
protected void initExport()
-
initReport
protected void initReport()
-
getPdfProducerFactory
protected PdfProducerFactory getPdfProducerFactory()
-
createPdfProducerContext
protected PdfProducerContext createPdfProducerContext()
-
createPdfTextRendererContext
protected PdfTextRendererContext createPdfTextRendererContext(JRPrintText text, JRStyledText styledText, Locale textLocale)
-
createPdfProducer
protected PdfProducer createPdfProducer()
-
exportReportToStream
protected void exportReportToStream(OutputStream os) throws JRException
- Throws:
JRException
-
writePageAnchor
protected void writePageAnchor(int pageIndex)
-
setPageSize
protected void setPageSize(JRPrintPage page) throws JRException, IOException
- Throws:
JRException
IOException
-
exportPage
protected void exportPage(JRPrintPage page) throws JRException, IOException
- Throws:
JRException
IOException
-
exportElements
protected void exportElements(Collection<JRPrintElement> elements) throws IOException, JRException
- Throws:
IOException
JRException
-
exportLine
protected void exportLine(JRPrintLine line)
-
exportRectangle
protected void exportRectangle(JRPrintRectangle rectangle)
-
exportEllipse
protected void exportEllipse(JRPrintEllipse ellipse)
-
exportImage
public void exportImage(JRPrintImage printImage) throws IOException, JRException
- Throws:
IOException
JRException
-
setHyperlinkInfo
protected void setHyperlinkInfo(PdfChunk chunk, JRPrintHyperlink link)
-
setReferenceHyperlink
protected void setReferenceHyperlink(PdfChunk chunk, JRPrintHyperlink link, String referenceURL)
-
getTextLocale
protected Locale getTextLocale(JRPrintText text)
-
getPhrase
protected void getPhrase(AttributedString as, String text, JRPrintText textElement, PdfPhrase phrase)
-
getChunk
protected PdfTextChunk getChunk(Map<AttributedCharacterIterator.Attribute,Object> attributes, String text, Locale locale)
-
hasUnderline
protected boolean hasUnderline(Map<AttributedCharacterIterator.Attribute,Object> textAttributes)
-
hasStrikethrough
protected boolean hasStrikethrough(Map<AttributedCharacterIterator.Attribute,Object> textAttributes)
-
setFont
protected void setFont(Map<AttributedCharacterIterator.Attribute,Object> attributes, Locale locale, boolean setFontLines, FontRecipient recipient)
Creates a PDF font.- Parameters:
attributes
- the text attributes of the fontlocale
- the locale for which to create the fontsetFontLines
- whether to set underline and strikethrough as font stylerecipient
- the font recipient
-
exportText
public void exportText(JRPrintText text)
-
getSingleForecolorAlpha
protected int getSingleForecolorAlpha(JRStyledText styledText)
-
exportFieldText
public void exportFieldText(JRPrintText text, PdfFieldTypeEnum fieldType)
-
getPdfFieldChoices
protected String[] getPdfFieldChoices(JRPrintText text)
-
exportFieldCheck
public void exportFieldCheck(JRPrintElement element)
-
exportFieldRadio
public void exportFieldRadio(JRPrintElement element) throws IOException
- Throws:
IOException
-
getFieldPen
protected JRPen getFieldPen(JRPrintElement element)
-
getTextRenderer
protected AbstractPdfTextRenderer getTextRenderer(JRPrintText text, JRStyledText styledText)
-
exportBox
protected void exportBox(JRLineBox box, JRPrintElement element)
-
exportPen
protected void exportPen(JRPen pen, JRPrintElement element)
-
exportTopPen
protected void exportTopPen(JRPen topPen, JRPen leftPen, JRPen rightPen, JRPrintElement element)
-
exportLeftPen
protected void exportLeftPen(JRPen topPen, JRPen leftPen, JRPen bottomPen, JRPrintElement element)
-
exportBottomPen
protected void exportBottomPen(JRPen leftPen, JRPen bottomPen, JRPen rightPen, JRPrintElement element)
-
exportRightPen
protected void exportRightPen(JRPen topPen, JRPen bottomPen, JRPen rightPen, JRPrintElement element)
-
registerFonts
protected static void registerFonts()
-
initBookmarks
protected void initBookmarks(List<ExporterInputItem> items)
-
addBookmark
protected void addBookmark(int level, String title, int x, int y)
-
setAnchor
protected void setAnchor(PdfChunk chunk, JRPrintAnchor anchor, JRPrintElement element)
-
exportFrame
public void exportFrame(JRPrintFrame frame) throws IOException, JRException
- Throws:
IOException
JRException
-
getCurrentPageFormat
protected PrintPageFormat getCurrentPageFormat()
-
getOffsetX
protected int getOffsetX()
Description copied from class:JRAbstractExporter
Returns the X axis offset used for element export.- Overrides:
getOffsetX
in classJRAbstractExporter<PdfReportConfiguration,PdfExporterConfiguration,OutputStreamExporterOutput,JRPdfExporterContext>
- Returns:
- the X axis offset
-
getOffsetY
protected int getOffsetY()
Description copied from class:JRAbstractExporter
Returns the Y axis offset used for element export.- Overrides:
getOffsetY
in classJRAbstractExporter<PdfReportConfiguration,PdfExporterConfiguration,OutputStreamExporterOutput,JRPdfExporterContext>
- Returns:
- the Y axis offset
-
exportGenericElement
protected void exportGenericElement(JRGenericPrintElement element)
-
getExporterKey
public String getExporterKey()
- Specified by:
getExporterKey
in classJRAbstractExporter<PdfReportConfiguration,PdfExporterConfiguration,OutputStreamExporterOutput,JRPdfExporterContext>
-
getExporterPropertiesPrefix
public String getExporterPropertiesPrefix()
Description copied from class:JRAbstractExporter
Returns the properties prefix for the current exporter.- Specified by:
getExporterPropertiesPrefix
in classJRAbstractExporter<PdfReportConfiguration,PdfExporterConfiguration,OutputStreamExporterOutput,JRPdfExporterContext>
- Returns:
- the properties prefix for the current exporter
-
getIntegerPermissions
public static int getIntegerPermissions(String permissions)
-
-