Class Pdf
- java.lang.Object
-
- com.github.jhonnymertz.wkhtmltopdf.wrapper.Pdf
-
public class Pdf extends Object
Represents a Pdf file and wraps up the wkhtmltopdf processing
-
-
Constructor Summary
Constructors Constructor Description Pdf()Deprecated.Pdf(WrapperConfig wrapperConfig)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddPage(String source, PageType type)Deprecated.Use the specific type method to a better semanticvoidaddPageFromFile(String source)Add a page from a file to the pdfvoidaddPageFromString(String source)Add a page from a HTML-based string to the pdfvoidaddPageFromUrl(String source)Add a page from an URL to the pdfvoidaddParam(Param param, Param... params)voidaddToc()voidaddTocParam(Param param, Param... params)voidcleanAllTempFiles()Cleans up all the files generated by the library.booleangetAllowMissingAssets()StringgetCommand()Gets the final wkhtmltopdf command as stringprotected String[]getCommandAsArray()Get command as array string Note: htmlAsString pages are first store into a temp file, then the location is used in the wkhtmltopdf command this is a workaround to avoid huge commandsbyte[]getPDF()Generates a PDF file as byte array from the wkhtmltopdf outputFilesaveAs(String path)Executes the wkhtmltopdf into standard out and captures the results.FilesaveAsDirect(String path)Executes the wkhtmltopdf saving the results directly to the specified file path.voidsetAllowMissingAssets()wkhtmltopdf often returns 1 to indicate some assets can't be found, this can occur for protocol less links or in other cases.voidsetSuccessValues(List<Integer> successValues)In standard process returns 0 means "ok" and any other value is an error.voidsetTempDirectory(File tempDirectory)Sets the temporary folder to store files during the process Default is provided by #File.createTempFile()voidsetTimeout(int timeout)Sets the timeout to wait while generating a PDF, in seconds
-
-
-
Constructor Detail
-
Pdf
@Deprecated public Pdf()
Deprecated.
-
Pdf
public Pdf(WrapperConfig wrapperConfig)
-
-
Method Detail
-
addPage
@Deprecated public void addPage(String source, PageType type)
Deprecated.Use the specific type method to a better semanticAdd a page to the pdf
-
addPageFromUrl
public void addPageFromUrl(String source)
Add a page from an URL to the pdf
-
addPageFromString
public void addPageFromString(String source)
Add a page from a HTML-based string to the pdf
-
addPageFromFile
public void addPageFromFile(String source)
Add a page from a file to the pdf
-
addToc
public void addToc()
-
setTimeout
public void setTimeout(int timeout)
Sets the timeout to wait while generating a PDF, in seconds
-
setAllowMissingAssets
public void setAllowMissingAssets()
wkhtmltopdf often returns 1 to indicate some assets can't be found, this can occur for protocol less links or in other cases. Sometimes you may want to reject these with an exception which is the default, but in other cases the PDF is fine for your needs. Call this method allow return values of 1.
-
getAllowMissingAssets
public boolean getAllowMissingAssets()
-
setSuccessValues
public void setSuccessValues(List<Integer> successValues)
In standard process returns 0 means "ok" and any other value is an error. However, wkhtmltopdf uses the return value to also return warning information which you may decide to ignore (@see setAllowMissingAssets)- Parameters:
successValues- The full list of process return values you will accept as a 'success'.
-
setTempDirectory
public void setTempDirectory(File tempDirectory)
Sets the temporary folder to store files during the process Default is provided by #File.createTempFile()- Parameters:
tempDirectory-
-
saveAs
public File saveAs(String path) throws IOException, InterruptedException
Executes the wkhtmltopdf into standard out and captures the results.- Parameters:
path- The path to the file where the PDF will be saved.- Returns:
- Throws:
IOExceptionInterruptedException
-
saveAsDirect
public File saveAsDirect(String path) throws IOException, InterruptedException
Executes the wkhtmltopdf saving the results directly to the specified file path.- Parameters:
path- The path to the file where the PDF will be saved.- Returns:
- the pdf file saved
- Throws:
IOException- when not able to save the fileInterruptedException- when the PDF generation process got interrupted
-
getPDF
public byte[] getPDF() throws IOException, InterruptedException, PDFExportExceptionGenerates a PDF file as byte array from the wkhtmltopdf output- Returns:
- the PDF file as a byte array
- Throws:
IOException- when not able to save the fileInterruptedException- when the PDF generation process got interruptedPDFExportException- when the wkhtmltopdf process fails
-
getCommandAsArray
protected String[] getCommandAsArray() throws IOException
Get command as array string Note: htmlAsString pages are first store into a temp file, then the location is used in the wkhtmltopdf command this is a workaround to avoid huge commands- Returns:
- the wkhtmltopdf command as string array
- Throws:
IOException- when not able to save temporary files from htmlAsString
-
cleanAllTempFiles
public void cleanAllTempFiles()
Cleans up all the files generated by the library.
-
getCommand
public String getCommand() throws IOException
Gets the final wkhtmltopdf command as string- Returns:
- the generated command from params
- Throws:
IOException- when not able to save temporary files from htmlAsString
-
-