Skip to content

Instantly share code, notes, and snippets.

@rsoika
Created September 14, 2021 14:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsoika/0cae2fa63a565ec4698bce13f243118d to your computer and use it in GitHub Desktop.
Save rsoika/0cae2fa63a565ec4698bce13f243118d to your computer and use it in GitHub Desktop.
HTML to PDF Converter - Java Client
package org.rsoika;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
/**
* Gotenberg is a Docker-powered stateless API for converting HTML, Markdown and
* Office documents to PDF.
* <p>
* This java client can be used to send a HTML file to the docker service to
* receive a PDF file based on the Gotenberg API. This implementation uses pure
* java based on JDK 1.8 and did not depend on any external library. You can use
* this client on your own code base or as a scaffold for a custom
* implementation.
* <p>
* The data is expected in UTF-8 encoding
*
* @author rsoika
* @version 1.0
* @see https://github.com/thecodingmachine/gotenberg/
* @see https://www.codejava.net/java-se/networking/upload-files-by-sending-multipart-request-programmatically
*
*/
public class GotenbergClient {
private static final String LINE_FEED = "\r\n";
public static byte[] convertHTML(String gotenbertEndpoint, InputStream inputStream) throws IOException {
byte[] pdfResult;
String boundary;
// creates a unique boundary based on time stamp
boundary = "------------------------" + System.currentTimeMillis();
if (!gotenbertEndpoint.endsWith("/")) {
gotenbertEndpoint = gotenbertEndpoint + "/";
}
URL url = new URL(gotenbertEndpoint + "convert/html");
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
OutputStream outputStream = httpConn.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8), true);
// add html file...
writer.append("--" + boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"fileUpload\"; filename=\"index.html\"").append(LINE_FEED);
writer.append("Content-Type: text/html").append(LINE_FEED);
writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
writer.append(LINE_FEED);
writer.flush();
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
writer.append(LINE_FEED);
writer.flush();
writer.append("--" + boundary + "--").append(LINE_FEED);
writer.close();
// checks server's status code first
int status = httpConn.getResponseCode();
System.out.println("http con response code=" + status);
if (status == HttpURLConnection.HTTP_OK) {
InputStream in = null;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
in = httpConn.getInputStream();
byte[] block = new byte[1024];
int length;
while ((length = in.read(block)) != -1) {
bout.write(block, 0, length);
}
in.close();
pdfResult= bout.toByteArray();
httpConn.disconnect();
} else {
throw new IOException("Server connection failed - status: " + status);
}
return pdfResult;
}
}
@rsoika
Copy link
Author

rsoika commented Sep 14, 2021

This is an example how to trigger the Gotenberg service from Java.
The code is a pure java client which can be used to access the gotenberg service from a java application.

@mxmesaby
Copy link

Hi Ralph
it does not work with the gotenberg version 7.5. the file generated has a pdf version 1.4, but it is not compliant, it cant be opened with a pdf reader. could you test against this version 7.5 please?

@cherfia
Copy link

cherfia commented Oct 16, 2022

@mxmesaby I made a Java library that works with the latest version of Gotenberg. However, it is still a WIP, but you can always experiment with it. Here's the link to the library. Please try it out and let me know if it solves your issue.

@rsoika
Copy link
Author

rsoika commented Oct 17, 2022

Cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment