Skip to content

Commit

Permalink
fix template, improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
klahap committed Sep 12, 2024
1 parent f1aaafa commit 4f4b9c1
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:
context: .
push: true
platforms: linux/amd64,linux/arm64
build-args: |
GIT_TAG_VERSION=${{ github.ref_name }}
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/web2pdf:latest
${{ secrets.DOCKERHUB_USERNAME }}/web2pdf:${{ github.ref_name }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
src/main/resources/banner.txt

.DS_Store

Expand Down
24 changes: 15 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
FROM gradle:8.10.0-jdk21 as builder
ARG GIT_TAG_VERSION
ENV GIT_TAG_VERSION=${GIT_TAG_VERSION}
WORKDIR /app
COPY . .
RUN ./gradlew bootJar

FROM alpine:3.19
FROM alpine:3.19 as chrome-base
RUN apk upgrade --no-cache --available \
&& apk add --no-cache \
chromium-swiftshader \
openjdk21-jre \
ttf-freefont \
font-noto-emoji \
fontconfig \
&& fc-cache -f
&& fc-cache -f \
&& mkdir -p /usr/src/app \
&& adduser -D chrome
RUN chown -R chrome:chrome /usr/src/app

# additional fonts
# RUN apk add --no-cache \
Expand All @@ -24,19 +28,21 @@ RUN apk upgrade --no-cache --available \
# font-noto-extra \
# && fc-cache -f

RUN mkdir -p /usr/src/app \
&& adduser -D chrome \
&& chown -R chrome:chrome /usr/src/app

USER chrome
WORKDIR /usr/src/app

ENV CHROME_BIN=/usr/bin/chromium-browser \
CHROME_PATH=/usr/lib/chromium/ \
CHROMIUM_FLAGS="--disable-software-rasterizer --disable-dev-shm-usage"

FROM chrome-base as chrome
USER chrome
EXPOSE 9222
ENTRYPOINT ["chromium-browser", "--headless", "--no-sandbox", "--remote-debugging-address=0.0.0.0", "--remote-debugging-port=9222", "--enable-features=ConversionMeasurement,AttributionReportingCrossAppWeb", "about:blank"]

FROM chrome-base
RUN apk add --no-cache openjdk21-jre
USER chrome
EXPOSE 8080
COPY --chmod=0755 ./main.sh .
COPY --from=builder /app/build/libs/web2pdf.jar ./web2pdf.jar

ENTRYPOINT ["./main.sh"]
8 changes: 8 additions & 0 deletions banner-template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
_ _
| | (_)
__ _ _ _ __ _ | |_ _
/ _` | | | | | / _` | | __| | |
| (_| | | |_| | | (_| | | |_ | |
\__, | \__,_| \__,_| \__| |_|
| |
|_|
26 changes: 16 additions & 10 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ plugins {
}

group = "io.github.goquati"
version = "0.0.1"

project.group
version = System.getenv("GIT_TAG_VERSION") ?: "1.0-SNAPSHOT"

repositories {
mavenCentral()
Expand All @@ -33,14 +31,10 @@ tasks {
}
}


val buildDir = layout.buildDirectory.get()
val projectDir = layout.projectDirectory

sourceSets {
main {
kotlin {
srcDir("$buildDir/generated/oas/src/main/kotlin")
srcDir("$rootDir/build/generated/oas/src/main/kotlin")
}
}
}
Expand Down Expand Up @@ -73,7 +67,7 @@ openApiGenerate {
generatorName.set("kotlin-spring")
library.set("spring-boot")
inputSpec.set("$rootDir/src/main/resources/oas.yaml")
outputDir.set("$buildDir/generated/oas")
outputDir.set("$rootDir/build/generated/oas")
apiPackage.set("$openApiGenDstRoot.api")
invokerPackage.set("$openApiGenDstRoot.invoker")
modelPackage.set("$openApiGenDstRoot.oas_model")
Expand All @@ -94,7 +88,19 @@ openApiGenerate {
)
}

tasks.compileKotlin { dependsOn(tasks.openApiGenerate) }
tasks.compileKotlin {
dependsOn(tasks.openApiGenerate)
dependsOn("createBanner")
}

tasks.register("createBanner") {
doLast {
val bannerTemplate = file("$rootDir/banner-template.txt").readText()
val length = bannerTemplate.split('\n').maxOf { it.length }
val content = bannerTemplate + "v$version".padStart(length) + "\n"
file("$rootDir/src/main/resources/banner.txt").writeText(content)
}
}

// start chrome for local development start:
// docker container run --rm -p 9222:9222 zenika/alpine-chrome --no-sandbox --remote-debugging-address=0.0.0.0 --remote-debugging-port=9222 about:blank
21 changes: 21 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: '3.8'

services:
chrome:
build:
dockerfile: Dockerfile
context: .
args:
DOCKER_BUILDKIT: 1
target: chrome
ports:
- "9222:9222"
quati-pdf:
build:
dockerfile: Dockerfile
context: .
args:
DOCKER_BUILDKIT: 1
GIT_TAG_VERSION: 0.0.1-DEV
ports:
- "8080:8080"
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.goquati.service

import io.github.goquati.LoggerDelegate
import io.github.goquati.Web2PdfException
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.updateAndGet
Expand All @@ -10,6 +11,7 @@ import org.hildan.chrome.devtools.protocol.RequestNotSentException
import org.hildan.chrome.devtools.sessions.BrowserSession
import org.hildan.chrome.devtools.sessions.PageSession
import org.hildan.chrome.devtools.sessions.newPage
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import java.net.ConnectException
import kotlin.time.Duration.Companion.seconds
Expand Down Expand Up @@ -47,8 +49,13 @@ class BrowserSessionService {
return try {
session.newPage()
} catch (e: RequestNotSentException) {
createNewBrowserSession(sessionId).newPage().also {
log.info("new websocket connection to browser created")
try {
createNewBrowserSession(sessionId).newPage().also {
log.info("new websocket connection to browser created")
}
} catch (e: Exception) {
log.error("cannot connect to chromium, restart server!")
throw Web2PdfException("Internal Server Error", status = HttpStatus.INTERNAL_SERVER_ERROR)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,8 @@ class Template2PdfService(
get() = InvoiceBaseDin5008(
TemplateInvoiceBaseDin5008Dto(
type = TemplateInvoiceBaseDin5008Dto.Type.A,
receiverInfo = listOf(
"Firma GmbH | Musterstraße 1 | 12345 Musterstadt",
"",
"",
),
sendBackAddress = "Firma GmbH | Musterstraße 1 | 12345 Musterstadt",
receiverInfo = listOf(),
receiver = listOf(
"Kundenname",
"Firmenadresse 123",
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
logging:
pattern:
console: "%d{yyyy-MM-dd HH:mm:ss.SSSXXX} %clr(%5p) [%15thread] %clr(%-40.40logger{39}){cyan} : %msg%n"
8 changes: 4 additions & 4 deletions src/main/resources/oas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,14 @@ components:
- A
- B
example: 'A'
sendBackAddress:
type: string
example: 'Firma GmbH | Musterstraße 1 | 12345 Musterstadt'
receiverInfo:
type: array
items:
type: string
example:
- 'Firma GmbH | Musterstraße 1 | 12345 Musterstadt'
- ''
- ''
example: []
receiver:
type: array
items:
Expand Down
15 changes: 11 additions & 4 deletions src/main/resources/templates/invoice-base-din-5008.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
}

.helper-box {
border: 0.001mm solid darkred;
outline: 0.001mm solid darkred;
}

.marker {
Expand All @@ -102,7 +102,7 @@
}

.helper-box {
border: none;
outline: none;
}

.invoice-box {
Expand Down Expand Up @@ -130,7 +130,14 @@
style="display: flex; justify-content: space-between; margin: {{#isTypeA}}27{{/isTypeA}}{{^isTypeA}}45{{/isTypeA}}mm 10mm 8.46mm 20mm">

<div class="helper-box" style="padding-left: 5mm; width: 80mm">
<div class="helper-box" style="height: 17.7mm; position: relative">
<div class="helper-box" style="height: 5mm ; position: relative">
{{#sendBackAddress}}
<div class="address-info-line" style="position: absolute; bottom: 0; border-bottom: 1px solid black; width: 100%">
{{sendBackAddress}}
</div>
{{/sendBackAddress}}
</div>
<div class="helper-box" style="height: 12.7mm; position: relative">
<div style="position: absolute; bottom: 0">
{{#receiverInfo}}
<div class="address-info-line">{{.}}</div>
Expand All @@ -144,7 +151,7 @@
</div>
</div>

<div class="helper-box" style="margin-top: 5mm; min-height: 40mm; max-width: 75mm; text-align: right">
<div class="helper-box" style="margin-top: 5mm; min-height: 40mm; max-width: 75mm">
{{#sender}}
<div class="address-line">{{.}}</div>
{{/sender}}
Expand Down

0 comments on commit 4f4b9c1

Please sign in to comment.