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 the
JRPdfExporter class in JasperReports is
one of the best exporters. The output it produces is almost of the same quality as that
produced by the JRGraphics2DExporter,
which is always the reference.
The JRPdfExporter 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 and isPdfEmbedded.
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 three fontName,
isBold, and isItalic font attributes, there must be an equivalent
combination of the PDF-related font attributes pdfFontName, pdfEncoding
and isPdfEmbedded.
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. The
JRPdfExporter internally acquires the needed PDF
font based on the font extension mechanism (see the getFont(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 the
isCreatingBatchModeBookmarks()
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 the isCompressed()
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 the
net.sf.jasperreports.export.pdf.force.linebreak.policy configuration
property
JavaScript 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 PDF Graphics2D
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 using
Renderable implementations,
which are most likely subclasses of the AbstractRenderToImageDataRenderer
class. SVG renderer implementations should be concerned only with
implementing the
public 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 the java.awt.Font objects used to draw text on the Graphics2D
context should have PDF-related text attributes embedded so that when rendered on a PDF
Graphics2D context, the exporter can make use of them. Embedding PDF-related text
attributes into the SVG means using the following text attributes when creating
java.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, and isPdfEmbedded)
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 the
net.sf.jasperreports.export.pdf.force.svg.shapes configuration property.
The isForceSvgShapes()
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.taggedconfiguration 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.Stringvalue; - using the
net.sf.jasperreports.export.pdf.tag.languageconfiguration 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 the JRPdfExporterTagHelper class.- Author:
- Teodor Danciu (teodord@users.sourceforge.net)
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classprotected static classprotected classNested classes/interfaces inherited from class net.sf.jasperreports.engine.JRAbstractExporter
JRAbstractExporter.BaseExporterContext, JRAbstractExporter.PageRange -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ColorSpaceprotected intstatic final Stringstatic final Stringstatic final Stringprotected static booleanprotected static final Stringstatic final Stringstatic final Stringprotected Map<String, Pair<PdfImage, ExifOrientationEnum>> protected PrintPageFormatstatic final StringThe exporter key, as used inGenericElementHandlerEnviroment.getElementHandler(JRGenericElementType, String).static final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final StringPrefix of properties that specify font directories for the PDF exporter.static final StringPrefix of properties that specify font files for the PDF exporter.static final Stringprotected PdfContentprotected PdfProducerprotected intprotected PdfImageprotected RenderersCacheprotected intprotected JRPdfExporterTagHelperFields 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 -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddBookmark(int level, String title, int x, int y) protected PdfProducerprotected PdfProducerContextprotected PdfTextRendererContextcreatePdfTextRendererContext(JRPrintText text, JRStyledText styledText, Locale textLocale) protected voidexportBottomPen(JRPen leftPen, JRPen bottomPen, JRPen rightPen, JRPrintElement element) protected voidexportBox(JRLineBox box, JRPrintElement element) protected voidexportElements(Collection<JRPrintElement> elements) protected voidexportEllipse(JRPrintEllipse ellipse) voidexportFieldCheck(JRPrintElement element) voidexportFieldRadio(JRPrintElement element) voidexportFieldText(JRPrintText text, PdfFieldTypeEnum fieldType) voidexportFrame(JRPrintFrame frame) protected voidvoidexportImage(JRPrintImage printImage) protected voidexportLeftPen(JRPen topPen, JRPen leftPen, JRPen bottomPen, JRPrintElement element) protected voidexportLine(JRPrintLine line) protected voidexportPage(JRPrintPage page) protected voidexportPen(JRPen pen, JRPrintElement element) protected voidexportRectangle(JRPrintRectangle rectangle) voidPerforms the export.protected voidprotected voidexportRightPen(JRPen topPen, JRPen bottomPen, JRPen rightPen, JRPrintElement element) voidexportText(JRPrintText text) protected voidexportTopPen(JRPen topPen, JRPen leftPen, JRPen rightPen, JRPrintElement element) protected PdfTextChunkprotected Class<PdfExporterConfiguration> protected PrintPageFormatReturns the properties prefix for the current exporter.protected JRPengetFieldPen(JRPrintElement element) static intgetIntegerPermissions(String permissions) protected Class<PdfReportConfiguration> protected intReturns the X axis offset used for element export.protected intReturns the Y axis offset used for element export.protected String[]protected PdfProducerFactoryprotected voidgetPhrase(AttributedString as, String text, JRPrintText textElement, PdfPhrase phrase) protected PdfImageprotected intgetSingleForecolorAlpha(JRStyledText styledText) protected LocalegetTextLocale(JRPrintText text) protected AbstractPdfTextRenderergetTextRenderer(JRPrintText text, JRStyledText styledText) protected booleanhasStrikethrough(Map<AttributedCharacterIterator.Attribute, Object> textAttributes) protected booleanhasUnderline(Map<AttributedCharacterIterator.Attribute, Object> textAttributes) protected voidinitBookmarks(List<ExporterInputItem> items) protected voidprotected voidprotected static voidprotected voidsetAnchor(PdfChunk chunk, JRPrintAnchor anchor, JRPrintElement element) protected voidsetFont(Map<AttributedCharacterIterator.Attribute, Object> attributes, Locale locale, boolean setFontLines, FontRecipient recipient) Creates a PDF font.protected booleansetHyperlinkInfo(PdfChunk chunk, JRPrintHyperlink link) protected voidsetPageSize(JRPrintPage page) protected booleansetReferenceHyperlink(PdfChunk chunk, JRPrintHyperlink link, String referenceURL) protected voidwritePageAnchor(int pageIndex) 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 Details
-
PDF_EXPORTER_PROPERTIES_PREFIX
- See Also:
-
EXCEPTION_MESSAGE_KEY_DOCUMENT_ERROR
- See Also:
-
EXCEPTION_MESSAGE_KEY_FONT_LOADING_ERROR
- See Also:
-
EXCEPTION_MESSAGE_KEY_REPORT_GENERATION_ERROR
- See Also:
-
PDF_FONT_FILES_PREFIX
Prefix of properties that specify font files for the PDF exporter.- See Also:
-
PDF_FONT_DIRS_PREFIX
Prefix of properties that specify font directories for the PDF exporter.- See Also:
-
PDF_FIELD_TYPE
- See Also:
-
PDF_FIELD_TEXT_MULTILINE
- See Also:
-
PDF_FIELD_VALUE
- See Also:
-
PDF_FIELD_CHECK_TYPE
- See Also:
-
PDF_FIELD_NAME
- See Also:
-
PDF_FIELD_CHECKED
- See Also:
-
PDF_FIELD_READ_ONLY
- See Also:
-
PDF_FIELD_BORDER_STYLE
- See Also:
-
PDF_FIELD_CHOICE_SEPARATORS
- See Also:
-
PDF_FIELD_CHOICES
- See Also:
-
PDF_FIELD_COMBO_EDIT
- See Also:
-
LEGACY_TARGET_BLANK_LINKS
- See Also:
-
LEGACY_TEXT_MEASURING_FIX
- See Also:
-
PDF_EXPORTER_KEY
The exporter key, as used inGenericElementHandlerEnviroment.getElementHandler(JRGenericElementType, String).- See Also:
-
PDF_PRODUCER_FACTORY_PROPERTY
- See Also:
-
JR_PAGE_ANCHOR_PREFIX
- See Also:
-
fontsRegistered
protected static boolean fontsRegistered -
pdfProducer
-
pdfContent
-
tagHelper
-
cmykColorSpace
-
reportIndex
protected int reportIndex -
pageFormat
-
crtDocumentPageNumber
protected int crtDocumentPageNumber -
permissions
protected int permissions -
renderersCache
-
loadedImagesMap
-
pxImage
-
-
Constructor Details
-
JRPdfExporter
public JRPdfExporter()- See Also:
-
JRPdfExporter
-
-
Method Details
-
getConfigurationInterface
-
getItemConfigurationInterface
-
getPxImage
-
exportReport
Description copied from interface:ExporterPerforms the export.- Specified by:
exportReportin interfaceExporter<ExporterInput,PdfReportConfiguration, PdfExporterConfiguration, OutputStreamExporterOutput> - Specified by:
exportReportin classJRAbstractExporter<PdfReportConfiguration,PdfExporterConfiguration, OutputStreamExporterOutput, JRPdfExporterContext> - Throws:
JRException
-
initExport
protected void initExport() -
initReport
protected void initReport() -
getPdfProducerFactory
-
createPdfProducerContext
-
createPdfTextRendererContext
protected PdfTextRendererContext createPdfTextRendererContext(JRPrintText text, JRStyledText styledText, Locale textLocale) -
createPdfProducer
-
exportReportToStream
- Throws:
JRException
-
writePageAnchor
protected void writePageAnchor(int pageIndex) -
setPageSize
- Throws:
JRExceptionIOException
-
exportPage
- Throws:
JRExceptionIOException
-
exportElements
- Throws:
IOExceptionJRException
-
exportLine
-
exportRectangle
-
exportEllipse
-
exportImage
- Throws:
IOExceptionJRException
-
setHyperlinkInfo
-
setReferenceHyperlink
-
getTextLocale
-
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
-
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
-
getSingleForecolorAlpha
-
exportFieldText
-
getPdfFieldChoices
-
exportFieldCheck
-
exportFieldRadio
- Throws:
IOException
-
getFieldPen
-
getTextRenderer
-
exportBox
-
exportPen
-
exportTopPen
-
exportLeftPen
-
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
-
addBookmark
-
setAnchor
-
exportFrame
- Throws:
IOExceptionJRException
-
getCurrentPageFormat
-
getOffsetX
protected int getOffsetX()Description copied from class:JRAbstractExporterReturns the X axis offset used for element export.- Overrides:
getOffsetXin classJRAbstractExporter<PdfReportConfiguration,PdfExporterConfiguration, OutputStreamExporterOutput, JRPdfExporterContext> - Returns:
- the X axis offset
-
getOffsetY
protected int getOffsetY()Description copied from class:JRAbstractExporterReturns the Y axis offset used for element export.- Overrides:
getOffsetYin classJRAbstractExporter<PdfReportConfiguration,PdfExporterConfiguration, OutputStreamExporterOutput, JRPdfExporterContext> - Returns:
- the Y axis offset
-
exportGenericElement
-
getExporterKey
-
getExporterPropertiesPrefix
Description copied from class:JRAbstractExporterReturns the properties prefix for the current exporter.- Specified by:
getExporterPropertiesPrefixin classJRAbstractExporter<PdfReportConfiguration,PdfExporterConfiguration, OutputStreamExporterOutput, JRPdfExporterContext> - Returns:
- the properties prefix for the current exporter
-
getIntegerPermissions
-