Skip to content

Commit

Permalink
new release and minor fixes in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jhonny Mertz committed Oct 25, 2020
1 parent cf146d1 commit 2b3da9d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ Requirements
Running tests
------------

This repo has two kinds of tests: integration and unit test.
This repo has two kinds of tests: integration (dependent on `wkhtmltopdf`) and unit tests.

Use `mvn test -B` to run only the unit tests.
By default, `mvn clean install` runs all the tests. Use `mvn test -B` to run only the unit tests.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ If you are using Gradle/Maven, see example below:
In your `build.gradle`:
```groovy
dependencies {
compile 'com.github.jhonnymertz:java-wkhtmltopdf-wrapper:1.1.12-RELEASE'
compile 'com.github.jhonnymertz:java-wkhtmltopdf-wrapper:1.1.13-RELEASE'
}
```

Expand All @@ -26,7 +26,7 @@ In your `pom.xml`:
<dependency>
<groupId>com.github.jhonnymertz</groupId>
<artifactId>java-wkhtmltopdf-wrapper</artifactId>
<version>1.1.12-RELEASE</version>
<version>1.1.13-RELEASE</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.jhonnymertz</groupId>
<artifactId>java-wkhtmltopdf-wrapper</artifactId>
<version>1.1.12-RELEASE</version>
<version>1.1.13-RELEASE</version>
<packaging>jar</packaging>

<name>Java WkHtmlToPdf Wrapper</name>
Expand Down
31 changes: 25 additions & 6 deletions src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/Pdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.concurrent.*;

/**
* Represents a Pdf file
* Represents a Pdf file and wraps up the wkhtmltopdf processing
*/
public class Pdf {

Expand All @@ -51,6 +51,8 @@ public class Pdf {

private File tempDirectory;

private static String TEMPORARY_FILE_PREFIX = "java-wkhtmltopdf-wrapper";

private String outputFilename = null;

private List<Integer> successValues = new ArrayList<Integer>(Arrays.asList(0));
Expand Down Expand Up @@ -177,9 +179,9 @@ public File saveAs(String path) throws IOException, InterruptedException {
* Executes the wkhtmltopdf saving the results directly to the specified file path.
*
* @param path The path to the file where the PDF will be saved.
* @return
* @throws IOException
* @throws InterruptedException
* @return the pdf file saved
* @throws IOException when not able to save the file
* @throws InterruptedException when the PDF generation process got interrupted
*/
public File saveAsDirect(String path) throws IOException, InterruptedException {
File file = new File(path);
Expand All @@ -188,6 +190,14 @@ public File saveAsDirect(String path) throws IOException, InterruptedException {
return file;
}

/**
* Generates a PDF file as byte array from the wkhtmltopdf output
*
* @return the PDF file as a byte array
* @throws IOException when not able to save the file
* @throws InterruptedException when the PDF generation process got interrupted
* @throws PDFExportException when the wkhtmltopdf process fails
*/
public byte[] getPDF() throws IOException, InterruptedException, PDFExportException {

ExecutorService executor = Executors.newFixedThreadPool(2);
Expand Down Expand Up @@ -219,6 +229,15 @@ public byte[] getPDF() throws IOException, InterruptedException, PDFExportExcept
}
}

/**
* 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
*
* @return the wkhtmltopdf command as string array
* @throws IOException when not able to save temporary files from htmlAsString
*/
protected String[] getCommandAsArray() throws IOException {
List<String> commandLine = new ArrayList<String>();

Expand All @@ -241,7 +260,7 @@ protected String[] getCommandAsArray() throws IOException {
// wkhtmltopdf, this is a workaround to avoid huge commands
if (page.getFilePath() != null)
Files.deleteIfExists(Paths.get(page.getFilePath()));
File temp = File.createTempFile("java-wkhtmltopdf-wrapper" + UUID.randomUUID().toString(), ".html", tempDirectory);
File temp = File.createTempFile(TEMPORARY_FILE_PREFIX + UUID.randomUUID().toString(), ".html", tempDirectory);
FileUtils.writeStringToFile(temp, page.getSource(), "UTF-8");
page.setFilePath(temp.getAbsolutePath());
commandLine.add(temp.getAbsolutePath());
Expand Down Expand Up @@ -306,7 +325,7 @@ public void cleanAllTempFiles() {
* Gets the final wkhtmltopdf command as string
*
* @return the generated command from params
* @throws IOException
* @throws IOException when not able to save temporary files from htmlAsString
*/
public String getCommand() throws IOException {
return StringUtils.join(getCommandAsArray(), " ");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package com.github.jhonnymertz.wkhtmltopdf.wrapper.page;

/**
* The Page type accepted by wkhtmltopdf
*/
public enum PageType {

/**
* Html as string
*/
htmlAsString,

/**
* Url page
*/
url,

/**
* File page
*/
file
}

0 comments on commit 2b3da9d

Please sign in to comment.