Last active
October 18, 2019 07:19
-
-
Save mlesikov/04c13dc66f6b8e624ae5b283f0e089ba to your computer and use it in GitHub Desktop.
Gottenberg simple Kotlin http client. Using google-http-client.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.clouway.tools.clients | |
import com.google.api.client.http.ByteArrayContent | |
import com.google.api.client.http.GenericUrl | |
import com.google.api.client.http.HttpHeaders | |
import com.google.api.client.http.HttpMediaType | |
import com.google.api.client.http.MultipartContent | |
import com.google.api.client.http.javanet.NetHttpTransport | |
import org.junit.Test | |
import java.io.File | |
import java.util.logging.Logger | |
/** | |
* @author Mihail Lesikov ([email protected]) | |
*/ | |
class GotenbergKClientTest { | |
private val log = Logger.getLogger(GotenbergKClientTest::class.java.name) | |
@Test | |
fun htmlToPdf() { | |
val footer = """<html> | |
<head> | |
<style> | |
body { | |
font-size: 8rem; | |
margin: 4rem auto; | |
font-color:red; | |
color: blue; | |
} | |
</style> | |
</head> | |
<body> | |
<p> blblbl | |
<span class="pageNumber"></span> of <span class="totalPages"></span> | |
</p> | |
</body> | |
</html>""" | |
val pdfBytes = htmlToPdf("http://localhost:3000", "Hello", header = "header", footer = footer) | |
File("hello.pdf").writeBytes(pdfBytes) | |
} | |
@Test | |
fun urlToPdf() { | |
val pdfBytes = urlToPdf("http://localhost:3000", "https://abv.bg") | |
File("ulrhello.pdf").writeBytes(pdfBytes) | |
} | |
private fun htmlToPdf(host: String, htmlPayload: String, options: Map<String, String> = mapOf( | |
"marginTop" to "0", | |
"marginBottom" to "0", | |
"marginLeft" to "0", | |
"marginRight" to "0" | |
), header:String = "", footer:String = "", assets:Map<String, ByteArray> = mapOf()): ByteArray { | |
log.info("url: $host") | |
log.info("POST: $htmlPayload") | |
val content = MultipartContent().setMediaType(HttpMediaType("multipart/form-data") | |
.setParameter("boundary", "__END_OF_PART__")) | |
val filePart = MultipartContent.Part( | |
HttpHeaders().set("Content-Disposition", "form-data; name=\"files\"; filename=\"index.html\""), | |
ByteArrayContent("text/html", htmlPayload.toByteArray())) | |
content.addPart(filePart) | |
if(header.isNotEmpty()) { | |
val headerPart = MultipartContent.Part( | |
HttpHeaders().set("Content-Disposition", "form-data; name=\"files\"; filename=\"header.html\""), | |
ByteArrayContent("text/html", header.toByteArray())) | |
content.addPart(headerPart) | |
} | |
if(footer.isNotEmpty()) { | |
val footerPart = MultipartContent.Part( | |
HttpHeaders().set("Content-Disposition", "form-data; name=\"files\"; filename=\"footer.html\""), | |
ByteArrayContent("text/html", footer.toByteArray())) | |
content.addPart(footerPart) | |
} | |
val options = mapOf( | |
"marginTop" to "0", | |
"marginBottom" to "0", | |
"marginLeft" to "0", | |
"marginRight" to "0" | |
) | |
options.forEach { | |
val part = MultipartContent.Part( | |
HttpHeaders().set("Content-Disposition", "form-data; name=\"${it.key}\";"), | |
ByteArrayContent("text/plain", it.value.toByteArray())) | |
content.addPart(part) | |
} | |
if (assets.isNotEmpty()) { | |
assets.forEach { (name, payload) -> | |
val assetPart = MultipartContent.Part( | |
HttpHeaders().set("Content-Disposition", "form-data; name=\"$name\";"), | |
ByteArrayContent("text/plain", payload)) | |
content.addPart(assetPart) | |
} | |
} | |
val response = NetHttpTransport() | |
.createRequestFactory {} | |
.buildPostRequest(GenericUrl("$host/convert/html"), content) | |
.setConnectTimeout(0).setReadTimeout(0) | |
.execute() | |
log.info("response ${response.statusCode}") | |
val pdfByteArray = response.content.readBytes() | |
return pdfByteArray | |
} | |
private fun urlToPdf(hostUrl: String, remoteURL: String, options: MutableMap<String, String> = mutableMapOf( | |
"marginTop" to "0", | |
"marginBottom" to "0", | |
"marginLeft" to "0", | |
"marginRight" to "0" | |
)): ByteArray { | |
log.info("url: $hostUrl") | |
log.info("POST: $remoteURL") | |
val content = MultipartContent().setMediaType(HttpMediaType("multipart/form-data") | |
.setParameter("boundary", "__END_OF_PART__")) | |
options["remoteURL"] = remoteURL | |
options.forEach { | |
val part = MultipartContent.Part( | |
HttpHeaders().set("Content-Disposition", "form-data; name=\"${it.key}\";"), | |
ByteArrayContent("text/plain", it.value.toByteArray())) | |
content.addPart(part) | |
} | |
val response = NetHttpTransport() | |
.createRequestFactory {} | |
.buildPostRequest(GenericUrl("$hostUrl/convert/url"), content) | |
.setConnectTimeout(0).setReadTimeout(0) | |
.execute() | |
log.info("response ${response.statusCode}") | |
val pdfByteArray = response.content.readBytes() | |
return pdfByteArray | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Start your Instance of Gotenberg like this: