Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
klahap committed Aug 22, 2024
0 parents commit 3c4db38
Show file tree
Hide file tree
Showing 24 changed files with 1,230 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.kotlin
.gradle
.idea
#build
Dockerfile
33 changes: 33 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Build and Push Docker Image

on:
push:
tags:
- '*.*.*'

jobs:
build-and-push:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and Push Docker Image
uses: docker/build-push-action@v5
with:
context: .
push: true
platforms: linux/amd64,linux/arm64
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/web2pdf:latest
${{ secrets.DOCKERHUB_USERNAME }}/web2pdf:${{ github.ref_name }}
40 changes: 40 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.gradle
.kotlin
.idea
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

.DS_Store

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/
32 changes: 32 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
FROM gradle:8.10.0-jdk21 as builder
WORKDIR /app
COPY . .
RUN ./gradlew bootJar

FROM alpine:3.19
RUN apk upgrade --no-cache --available \
&& apk add --no-cache \
chromium-swiftshader \
ttf-freefont \
font-noto-emoji \
&& apk add --no-cache \
--repository=https://dl-cdn.alpinelinux.org/alpine/edge/community \
font-wqy-zenhei \
&& apk --no-cache add openjdk21-jre

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"

EXPOSE 8080
COPY --chmod=0755 ./main.sh .
COPY --from=builder /app/build/libs/web2pdf.jar ./web2pdf.jar

ENTRYPOINT ["./main.sh"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 goquati

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
108 changes: 108 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# web2pdf

`web2pdf` is a lightweight REST API service that allows you to generate PDF files from web pages using Chromium. The service is implemented in Kotlin and provides both GET and POST endpoints, enabling flexible usage for different scenarios.

## Features

- **Chromium-based PDF generation:** Leverages Chromium's print-to-PDF functionality to ensure high-quality PDF output.
- **GET Endpoint:** Easily configure your print settings via a URL, which can be shared or called directly in the browser to generate a PDF.
- **POST Endpoint:** Designed for programmatic access, allowing more complex API interactions.

## Installation

The easiest way to get `web2pdf` up and running is by using the provided Docker image.

### Docker

1. Pull the Docker image:
```bash
docker pull goquati/web2pdf:latest
```

2. Run the container:
```bash
docker run -d -p 8080:8080 goquati/web2pdf:latest
```

This will start the `web2pdf` service on port `8080` of your local machine.

## Usage

### GET Endpoint

The GET endpoint allows you to generate a PDF by simply calling a URL. This is useful for quick, one-off conversions or sharing with others.

**Endpoint:** `/web2pdf`

**Example:**

```bash
GET /web2pdf?url=http://example.com&header[Authorization]=my-token&cookie[my-cookie]=my-cookie-value&options.landscape=false&options.printBackground=true
```

**Query Parameters:**

- `url`: (Required) The URL of the webpage you want to convert to PDF.
- `header`: (Optional) HTTP headers to include in the request.
- `cookie`: (Optional) Cookies to include in the request.
- `options`: (Optional) PDF print options such as `landscape`, `printBackground`, `scale`, `paperWidth`, `paperHeight`, etc.

**Response:**

- **200 OK**: Returns the generated PDF.
- **400 Bad Request**: Returns an error response if the request is invalid.

### POST Endpoint

The POST endpoint is ideal for more API-like usage, where you need to pass more complex data structures.

**Endpoint:** `/web2pdf`

**Example:**

```bash
POST /web2pdf
Content-Type: application/json

{
"url": "http://example.com",
"headers": {
"Authorization": "my-token"
},
"cookies": {
"my-cookie": "my-cookie-value"
},
"options": {
"landscape": false,
"printBackground": true
}
}
```

**Request Body:**

- `url`: (Required) The URL of the webpage you want to convert to PDF.
- `headers`: (Optional) HTTP headers to include in the request.
- `cookies`: (Optional) Cookies to include in the request.
- `options`: (Optional) PDF print options, similar to the GET endpoint.

**Response:**

- **200 OK**: Returns the generated PDF.
- **400 Bad Request**: Returns an error response if the request is invalid.

## OpenAPI Specification

The full OpenAPI specification for the `web2pdf` service is available in the repository at [src/main/resources/oas.yaml](./src/main/resources/oas.yaml). This specification provides detailed information about the endpoints, request/response formats, and available parameters.

## Contributing

Contributions are welcome! Please fork the repository and submit a pull request.

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.

---

With `web2pdf`, you can easily convert web pages to PDFs with just a URL or a simple API call. Whether you're looking to automate the process or need quick access to a PDF version of a page, `web2pdf` has you covered.
99 changes: 99 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion

plugins {
id("org.openapi.generator") version "7.6.0"
id("org.springframework.boot") version "3.3.0"
id("io.spring.dependency-management") version "1.1.5"
kotlin("plugin.spring") version "2.0.10"
kotlin("jvm") version "2.0.10"
kotlin("plugin.serialization") version "2.0.10"
}

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

project.group

repositories {
mavenCentral()
}

java {
sourceCompatibility = JavaVersion.VERSION_21
}

tasks {
jar {
enabled = true
}
bootJar {
archiveFileName.set("web2pdf.jar")
mainClass.set("${project.group}.Web2PdfApplicationKt")
}
}


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

sourceSets {
main {
kotlin {
srcDir("$buildDir/generated/oas/src/main/kotlin")
}
}
}

kotlin {
compilerOptions {
freeCompilerArgs.add("-Xjsr305=strict")
freeCompilerArgs.add("-Xcontext-receivers")
jvmTarget.set(JvmTarget.JVM_21)
languageVersion.set(KotlinVersion.KOTLIN_2_0)
}
}

dependencies {
implementation("org.springdoc:springdoc-openapi-starter-webflux-ui:2.3.0")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
implementation("org.jetbrains.kotlin:kotlin-reflect:2.0.10")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
implementation("io.ktor:ktor-client-cio:2.3.12")
implementation("org.hildan.chrome:chrome-devtools-kotlin:6.3.0-1340018")
}

val openApiGenDstRoot = "de.smart.nexus.orchestrator"

openApiGenerate {
generatorName.set("kotlin-spring")
library.set("spring-boot")
inputSpec.set("$rootDir/src/main/resources/oas.yaml")
outputDir.set("$buildDir/generated/oas")
apiPackage.set("$openApiGenDstRoot.api")
invokerPackage.set("$openApiGenDstRoot.invoker")
modelPackage.set("$openApiGenDstRoot.oas_model")
validateSpec.set(true)
modelNameSuffix.set("Dto")
additionalProperties.set(mapOf("removeEnumValuePrefix" to "false"))
templateDir.set("$projectDir/src/main/resources/oas-templates")
configOptions.set(
mapOf(
"appendRequestToHandler" to "true",
"interfaceOnly" to "true",
"sourceFolder" to "src/main/kotlin",
"useTags" to "true",
"reactive" to "true",
"skipDefaultInterface" to "true",
"useSpringBoot3" to "true",
"enumPropertyNaming" to "UPPERCASE",
)
)
}

tasks.compileKotlin { dependsOn(tasks.openApiGenerate) }

// 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
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kotlin.code.style=official
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 3c4db38

Please sign in to comment.