Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Update Form.tsx to solve issue #1 #38

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 2
updates:

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"

- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
open-pull-requests-limit: 3

- package-ecosystem: "pip"
directory: "/kaaf"
schedule:
interval: "weekly"
14 changes: 7 additions & 7 deletions .github/workflows/deploy-refusjon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ name: Deploy refusjon container to Azure

on:
push:
branches: [ refusjon-master ]
branches: refusjon-master

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- uses: azure/docker-login@v1
with:
login-server: ntnuiskjema.azurecr.io
login-server: ntnuiservices.azurecr.io
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}

- run: |
docker build . -t ntnuiskjema.azurecr.io/skjema-refusjon:latest
docker push ntnuiskjema.azurecr.io/skjema-refusjon:latest
docker build . -t ntnuiservices.azurecr.io/skjema-refusjon:latest
docker push ntnuiservices.azurecr.io/skjema-refusjon:latest

- uses: azure/webapps-deploy@v2
with:
app-name: 'ntnuirefusjon'
app-name: 'ntnui-skjema-refusjon'
publish-profile: ${{ secrets.REFUSJON_AZURE_WEBAPP_PUBLISH_PROFILE }}
images: 'ntnuiskjema.azurecr.io/skjema-refusjon:latest'
images: 'ntnuiservices.azurecr.io/skjema-refusjon:latest'
14 changes: 7 additions & 7 deletions .github/workflows/deploy-reise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ name: Deploy reise container to Azure

on:
push:
branches: [ reise-master ]
branches: reise-master

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- uses: azure/docker-login@v1
with:
login-server: ntnuiskjema.azurecr.io
login-server: ntnuiservices.azurecr.io
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}

- run: |
docker build . -t ntnuiskjema.azurecr.io/skjema-reise:latest
docker push ntnuiskjema.azurecr.io/skjema-reise:latest
docker build . -t ntnuiservices.azurecr.io/skjema-reise:latest
docker push ntnuiservices.azurecr.io/skjema-reise:latest

- uses: azure/webapps-deploy@v2
with:
app-name: 'ntnuireise'
app-name: 'ntnui-skjema-reise'
publish-profile: ${{ secrets.REISE_AZURE_WEBAPP_PUBLISH_PROFILE }}
images: 'ntnuiskjema.azurecr.io/skjema-reise:latest'
images: 'ntnuiservices.azurecr.io/skjema-reise:latest'
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ webapp/.next/
webapp/out/
venv/
kaaf/__pycache__/
.vscode
.vscode
.env
output.pdf
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
FROM openfaas/of-watchdog:0.8.2 as watchdog
FROM python:3.7-slim AS build-backend

RUN apt-get update && apt-get install -y poppler-utils
FROM ghcr.io/openfaas/of-watchdog:0.9.11 as watchdog
FROM python:3.11 AS build-backend

WORKDIR /app

Expand All @@ -10,6 +8,8 @@ RUN chmod +x /usr/bin/fwatchdog

COPY ./kaaf/req.txt ./kaaf/req.txt

RUN python -m pip install --upgrade pip

RUN pip install --no-cache-dir -r kaaf/req.txt

FROM node:16-alpine3.11 AS build-frontend
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@ To run just the frontend:
To run the backend/everything:

- Make a virtual env with `python -m venv venv`
- Enter the env with `source venv/bin/activate`
- Enter the env with (Unix) `source venv/bin/activate` or (Windows) `source venv/Scripts/activate`
- Make sure you are using latest pip with `python -m pip install --upgrade pip`
- Install packages with `pip install -r kaaf/req.txt`
- Start the server with `python kaaf/server.py`
- If the frontend is exported (`yarn export`), the webapp will be available at `localhost:5000` when running `server.py`

> One of the packages (pdf2image) will require poppler to work correctly with tmp files. Most linux distros come with this.
> For MacOS `brew install poppler`

### Generating PDFs

It might be nice to be able to quickly generate PDFs when developing, without having to start up everything. To do this you can run:

```python
python kaaf/generate-example.py signature.png output.pdf image0.png image1.png ...
python kaaf/generate-example.py signature.png attachment1.png attachment2.pdf ...
```

Where `signature.png` and `imageN.png` are paths to image files (the latter images are optional)
Where `signature.png` and `attachmentN.XYZ` are paths to image files.

## Environment variables

While developing locally, you can temporarily add environment variables to the Dockerfile, such as `ENV MAIL_ADDRESS="[email protected]"`, or by creating an **.env** file with `KEY=VALUE` pairs separated by a newline.

| Variable | Function |
| --------------- | -------------------------------------------- |
| `MAIL_ADDRESS` | Set the mail address for generated receipts |
| `MAIL_PASSWORD` | Password for the mail account |
| `SERVICE_ACCOUNT_STR` | Google service account string |
| `MAIL_ADDRESS` | Set the mail address for generated receipts |
| `ENVIRONMENT` | Set to "production" for sentry errors |
| `SENTRY_DSN` | Ingest errors to sentry |
Binary file added images/16bit-depth.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/dragvoll.HEIC
Binary file not shown.
Binary file added images/example-old-output.pdf
Binary file not shown.
Binary file added images/example-signature.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
95 changes: 62 additions & 33 deletions kaaf/generate-example.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,78 @@
import argparse
import base64
import sys
import os
import magic

from handler import create_pdf, modify_data
from handler import create_pdf

default_data = {
"date": "2020-12-27",
"amount": "69 kr",
"name": "Mats",
"accountNumber": "010101010101",
"committee": "Hovedstyret",
"occasion": "Teste litt",
"comment": "pls work",
test_data = {
"name": "John Doe",
"mailfrom": "[email protected]",
"committee": "Sprint",
"accountNumber": "123456789",
"amount": "69.69",
"date": "2023-05-17",
"occasion": "Expense reimbursement",
"comment": "Some comment\n with multiple\n newlines",
}

if len(sys.argv) < 3:
print("Error: Missing arguments")
print(f"Usage: python3 {sys.argv[0]} signature_file, attachment_files")
print(f"Output: output.pdf")
sys.exit(1)

def main(data, out):
data = modify_data(data)
# Parse the command line arguments
signature_file = sys.argv[1]
attachment_files = sys.argv[2:]

pdf = create_pdf(data)
allowed_extensions = {".pdf", ".jpg", ".jpeg", ".png", ".gif", ".heic"}

with open(out, "wb") as f:
f.write(pdf.encode("latin-1"))

print("Done!")
def is_valid_file_extension(file_path, allowed_extensions):
_, file_extension = os.path.splitext(file_path)
return file_extension.lower() in allowed_extensions


def encode_image(img):
with open(img, "rb") as f:
b64 = base64.b64encode(f.read()).decode("ascii")
return f'data:image/{img.split(".")[-1]};base64,{b64}'
# Return exception if signature or attachment files are not valid
if not is_valid_file_extension(signature_file, allowed_extensions):
raise Exception(f"Invalid signature file extension: {signature_file}")
for file_path in attachment_files:
if not is_valid_file_extension(file_path, allowed_extensions):
raise Exception(f"Invalid attachment file extension: {file_path}")

# Convert signature file to base64:image/png
with open(signature_file, "rb") as f:
signature = f.read()
signature = base64.b64encode(signature).decode("utf-8")
signature = f"data:image/png;base64,{signature}"

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("signature", help="Path to signature")
parser.add_argument("out", help="Path to the generated pdf")
parser.add_argument(
"images", nargs=argparse.REMAINDER, default=[], help="Paths to images"
)
args = parser.parse_args()
# Check file type and convert attachment files to base64 with MIME type
attachments = []
for file_path in attachment_files:
with open(file_path, "rb") as f:
# Read the file as bytes
file_data = f.read()

data = {
**default_data,
"signature": encode_image(args.signature),
"images": [encode_image(img) for img in args.images],
}
# Detect the filetype using python-magic
file_type = magic.from_buffer(file_data, mime=True)

main(data, args.out)
# Convert the file data to base64
file_data = base64.b64encode(file_data).decode("utf-8")

# Add the filetype prefix to the base64 string
file_data = f"data:{file_type};base64,{file_data}"

# Check if the filetype is one of the allowed ones
allowed_types = ["application/pdf", "image/jpeg", "image/png", "image/heic"]
if file_type in allowed_types:
# Append the file data to the attachments list
attachments.append(file_data)

print(f"Signature: {signature[:50]}")
for attachment in attachments:
print(f"Attachment: {attachment[:50]}")

# Call the create_pdf function to generate the PDF
create_pdf(test_data, signature, attachments)
Loading