Package com.itextpdf.io.util
Class GhostscriptHelper
- java.lang.Object
-
- com.itextpdf.io.util.GhostscriptHelper
-
public class GhostscriptHelper extends java.lang.ObjectA utility class that is used as an interface to run 3rd-party tool Ghostscript. Ghostscript is an interpreter for the PostScript language and PDF files, it allows to render them as images.The Ghostscript needs to be installed independently on the system. This class provides a convenient way to run it by passing a terminal command. The command can either be specified explicitly or by a mean of environment variable
GHOSTSCRIPT_ENVIRONMENT_VARIABLE.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classGhostscriptHelper.GhostscriptExecutionExceptionExceptions thrown when errors occur during generation and comparison of images obtained on the basis of pdf files.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringGHOSTSCRIPT_ENVIRONMENT_VARIABLEThe name of the environment variable with the command to execute Ghostscript operations.(package private) static java.lang.StringGHOSTSCRIPT_KEYWORDprivate static java.lang.StringGHOSTSCRIPT_PARAMSprivate java.lang.StringgsExecprivate static java.util.regex.PatternPAGE_LIST_REGEXprivate static java.lang.StringPAGE_NUMBER_PATTERNprivate static java.lang.StringRENDERED_IMAGE_EXTENSIONprivate static java.lang.StringTEMP_FILE_PREFIX
-
Constructor Summary
Constructors Constructor Description GhostscriptHelper()Creates new instance that will rely on Ghostscript execution command defined byGHOSTSCRIPT_ENVIRONMENT_VARIABLEenvironment variable.GhostscriptHelper(java.lang.String newGsExec)Creates new instance that will rely on Ghostscript execution command defined as passed argument.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) static java.lang.StringformatImageNumber(int pageNumber)java.lang.StringgetCliExecutionCommand()Returns a command that is used to run the utility.voidrunGhostScriptImageGeneration(java.lang.String pdf, java.lang.String outDir, java.lang.String image)Runs Ghostscript to render the PDF's pages as PNG images.voidrunGhostScriptImageGeneration(java.lang.String pdf, java.lang.String outDir, java.lang.String image, java.lang.String pageList)Runs Ghostscript to render the PDF's pages as PNG images.(package private) static booleanvalidateImageFilePattern(java.lang.String imageFilePattern)(package private) static booleanvalidatePageList(java.lang.String pageList)
-
-
-
Field Detail
-
GHOSTSCRIPT_ENVIRONMENT_VARIABLE
public static final java.lang.String GHOSTSCRIPT_ENVIRONMENT_VARIABLE
The name of the environment variable with the command to execute Ghostscript operations.- See Also:
- Constant Field Values
-
GHOSTSCRIPT_KEYWORD
static final java.lang.String GHOSTSCRIPT_KEYWORD
- See Also:
- Constant Field Values
-
TEMP_FILE_PREFIX
private static final java.lang.String TEMP_FILE_PREFIX
- See Also:
- Constant Field Values
-
RENDERED_IMAGE_EXTENSION
private static final java.lang.String RENDERED_IMAGE_EXTENSION
- See Also:
- Constant Field Values
-
GHOSTSCRIPT_PARAMS
private static final java.lang.String GHOSTSCRIPT_PARAMS
- See Also:
- Constant Field Values
-
PAGE_NUMBER_PATTERN
private static final java.lang.String PAGE_NUMBER_PATTERN
- See Also:
- Constant Field Values
-
PAGE_LIST_REGEX
private static final java.util.regex.Pattern PAGE_LIST_REGEX
-
gsExec
private java.lang.String gsExec
-
-
Constructor Detail
-
GhostscriptHelper
public GhostscriptHelper()
Creates new instance that will rely on Ghostscript execution command defined byGHOSTSCRIPT_ENVIRONMENT_VARIABLEenvironment variable.
-
GhostscriptHelper
public GhostscriptHelper(java.lang.String newGsExec)
Creates new instance that will rely on Ghostscript execution command defined as passed argument.- Parameters:
newGsExec- the Ghostscript execution command; if null - environment variables will be used instead
-
-
Method Detail
-
getCliExecutionCommand
public java.lang.String getCliExecutionCommand()
Returns a command that is used to run the utility. This command doesn't contain command parameters. Parameters are added on specific methods invocation.- Returns:
- a string command
-
runGhostScriptImageGeneration
public void runGhostScriptImageGeneration(java.lang.String pdf, java.lang.String outDir, java.lang.String image) throws java.io.IOException, java.lang.InterruptedExceptionRuns Ghostscript to render the PDF's pages as PNG images.Note, that this method may create temporary directory and files.
- Parameters:
pdf- Path to the PDF file to be renderedoutDir- Path to the output directory, in which the rendered pages will be storedimage- String which defines the name of the resultant images. This string will be concatenated with the number of the rendered page from the start of the PDF in "-%03d" format, e.g. "-011" for the eleventh rendered page and so on. This number may not correspond to the actual page number: for example, if the passed pageList equals to "5,3", then images with postfixes "-001.png" and "-002.png" will be created: the former for the third page, the latter for the fifth page. "%" sign in the passed name is prohibited.- Throws:
java.io.IOException- if there are file's reading/writing issuesjava.lang.InterruptedException- if there is thread interruption while executing GhostScript.
-
runGhostScriptImageGeneration
public void runGhostScriptImageGeneration(java.lang.String pdf, java.lang.String outDir, java.lang.String image, java.lang.String pageList) throws java.io.IOException, java.lang.InterruptedExceptionRuns Ghostscript to render the PDF's pages as PNG images.Note, that this method may create temporary directory and files.
- Parameters:
pdf- Path to the PDF file to be renderedoutDir- Path to the output directory, in which the rendered pages will be storedimage- String which defines the name of the resultant images. This string will be concatenated with the number of the rendered page from the start of the PDF in "-%03d" format, e.g. "-011" for the eleventh rendered page and so on. This number may not correspond to the actual page number: for example, if the passed pageList equals to "5,3", then images with postfixes "-001.png" and "-002.png" will be created: the former for the third page, the latter for the fifth page. "%" sign in the passed name is prohibited.pageList- String with numbers of the required pages to be rendered as images. This string should be formatted as a string with numbers, separated by commas, without whitespaces. Can be null, if it is required to render all the PDF's pages.- Throws:
java.io.IOException- if there are file's reading/writing issuesjava.lang.InterruptedException- if there is thread interruption while executing GhostScript.
-
validatePageList
static boolean validatePageList(java.lang.String pageList)
-
validateImageFilePattern
static boolean validateImageFilePattern(java.lang.String imageFilePattern)
-
formatImageNumber
static java.lang.String formatImageNumber(int pageNumber)
-
-