-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
238 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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
2 changes: 1 addition & 1 deletion
2
...ain/kotlin/io/github/klahap/Exceptions.kt → ...in/kotlin/io/github/goquati/Exceptions.kt
This file contains 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
2 changes: 1 addition & 1 deletion
2
...kotlin/io/github/klahap/LoggerDelegate.kt → ...otlin/io/github/goquati/LoggerDelegate.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package io.github.klahap | ||
package io.github.goquati | ||
|
||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
|
2 changes: 1 addition & 1 deletion
2
...in/io/github/klahap/Web2PdfApplication.kt → ...n/io/github/goquati/Web2PdfApplication.kt
This file contains 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
40 changes: 40 additions & 0 deletions
40
src/main/kotlin/io/github/goquati/api/Html2PdfController.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.github.goquati.api | ||
|
||
import de.smart.nexus.orchestrator.api.Html2pdfApi | ||
import de.smart.nexus.orchestrator.oas_model.Html2PdfRequestDto | ||
import io.github.goquati.LoggerDelegate | ||
import io.github.goquati.LoggerDelegate.Companion.measureExecutionTime | ||
import io.github.goquati.service.Html2PdfService | ||
import org.springframework.core.io.ByteArrayResource | ||
import org.springframework.core.io.Resource | ||
import org.springframework.http.MediaType | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.http.server.reactive.ServerHttpRequest | ||
import org.springframework.stereotype.Controller | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import java.util.* | ||
|
||
@Controller | ||
class Html2PdfController( | ||
private val html2PdfService: Html2PdfService, | ||
) : Html2pdfApi { | ||
override suspend fun convertHtml2Pdf( | ||
html2PdfRequestDto: Html2PdfRequestDto, | ||
serverHttpRequest: ServerHttpRequest | ||
): ResponseEntity<Resource> { | ||
return log.measureExecutionTime("generated PDF") { | ||
html2PdfService.generatePdf( | ||
data = html2PdfRequestDto.data, | ||
options = html2PdfRequestDto.options, | ||
) | ||
}.let { ResponseEntity.ok(ByteArrayResource(it)) } | ||
} | ||
|
||
@GetMapping("/html2pdf-data/{id}", produces = [MediaType.TEXT_HTML_VALUE]) | ||
suspend fun getHtml(@PathVariable id: UUID) = ResponseEntity.ok(html2PdfService.getHtml(id)) | ||
|
||
companion object { | ||
private val log by LoggerDelegate() | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/kotlin/io/github/goquati/api/Markdown2PdfController.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.github.goquati.api | ||
|
||
import de.smart.nexus.orchestrator.api.Markdown2pdfApi | ||
import de.smart.nexus.orchestrator.oas_model.Markdown2PdfRequestDto | ||
import io.github.goquati.LoggerDelegate | ||
import io.github.goquati.LoggerDelegate.Companion.measureExecutionTime | ||
import io.github.goquati.service.Markdown2PdfService | ||
import org.springframework.core.io.ByteArrayResource | ||
import org.springframework.core.io.Resource | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.http.server.reactive.ServerHttpRequest | ||
import org.springframework.stereotype.Controller | ||
|
||
@Controller | ||
class Markdown2PdfController( | ||
private val markdown2PdfService: Markdown2PdfService, | ||
) : Markdown2pdfApi { | ||
override suspend fun convertMarkdown2Pdf( | ||
markdown2PdfRequestDto: Markdown2PdfRequestDto, | ||
serverHttpRequest: ServerHttpRequest | ||
): ResponseEntity<Resource> { | ||
return log.measureExecutionTime("generated PDF") { | ||
markdown2PdfService.generatePdf( | ||
data = markdown2PdfRequestDto.data, | ||
options = markdown2PdfRequestDto.options, | ||
) | ||
}.let { ResponseEntity.ok(ByteArrayResource(it)) } | ||
} | ||
|
||
companion object { | ||
private val log by LoggerDelegate() | ||
} | ||
} |
This file contains 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
4 changes: 2 additions & 2 deletions
4
...b/klahap/service/BrowserSessionService.kt → .../goquati/service/BrowserSessionService.kt
This file contains 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
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/io/github/goquati/service/Html2PdfService.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.github.goquati.service | ||
|
||
import de.smart.nexus.orchestrator.oas_model.PdfPrintOptionsDto | ||
import io.github.goquati.Web2PdfException | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import org.springframework.core.env.Environment | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.stereotype.Service | ||
import java.util.UUID | ||
|
||
@Service | ||
class Html2PdfService( | ||
private val web2PdfService: Web2PdfService, | ||
private val environment: Environment, | ||
) { | ||
private val htmlCache = MutableStateFlow<MutableMap<UUID, String>>(mutableMapOf()) | ||
private val host | ||
get() = if (environment.activeProfiles.contains("local")) | ||
"host.docker.internal" | ||
else | ||
"localhost" | ||
|
||
suspend fun generatePdf( | ||
data: String, | ||
options: PdfPrintOptionsDto?, | ||
): ByteArray { | ||
val id = UUID.randomUUID()!! | ||
htmlCache.update { it[id] = data; it } | ||
try { | ||
return web2PdfService.generatePdf( | ||
url = "http://$host:8080/html2pdf-data/$id", | ||
options = options, | ||
) | ||
} finally { | ||
htmlCache.update { it.remove(id); it } | ||
} | ||
} | ||
|
||
fun getHtml(id: UUID) = htmlCache.value[id] | ||
?: throw Web2PdfException("html data not found", status = HttpStatus.NOT_FOUND) | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/io/github/goquati/service/Markdown2PdfService.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.github.goquati.service | ||
|
||
import de.smart.nexus.orchestrator.oas_model.PdfPrintOptionsDto | ||
import org.intellij.markdown.flavours.commonmark.CommonMarkFlavourDescriptor | ||
import org.intellij.markdown.html.HtmlGenerator | ||
import org.intellij.markdown.parser.MarkdownParser | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class Markdown2PdfService( | ||
private val html2PdfService: Html2PdfService, | ||
) { | ||
suspend fun generatePdf( | ||
data: String, | ||
options: PdfPrintOptionsDto?, | ||
) = html2PdfService.generatePdf( | ||
data = data.toHtml(), | ||
options = options, | ||
) | ||
|
||
private final val flavour = CommonMarkFlavourDescriptor() | ||
private final val markdownParser = MarkdownParser(flavour) | ||
|
||
private fun String.toHtml() = HtmlGenerator( | ||
markdownText = this, | ||
root = markdownParser.buildMarkdownTreeFromString(this), | ||
flavour = flavour, | ||
).generateHtml() | ||
} |
This file contains 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
This file contains 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