Skip to content

Commit

Permalink
feat: PDF-Exporter webhook for setting table td height
Browse files Browse the repository at this point in the history
  • Loading branch information
grigoriev committed Aug 12, 2024
1 parent 676828a commit 069499a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 92 deletions.
33 changes: 0 additions & 33 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,40 +1,7 @@
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
.idea/*

# code style config
!.idea/codeStyles
.idea/codeStyles/*
!.idea/codeStyles/Project.xml
!.idea/codeStyles/codeStyleConfig.xml

.vscode/*

target/
*.iml
dependency-reduced-pom.xml

src/main/resources/webapp/*-admin/html/about.html
README.html
9 changes: 0 additions & 9 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ All contributors must have an active Polarion license. An active Polarion licens
## <a id="asking-questions"></a>Asking questions
Do not know how something in this project works? Curious if this project can achieve your desired functionality? Please ask questions in this project discussions [here](../../discussions)

## <a id="what-should-i-know-before-i-get-started"></a>What should I know before I get started?

### <a id="tools-and-packages"></a>Tools and Packages
All extensions provided by SBB Polarion Team can be built, tested and packaged using Maven.
It is only possible when the dependencies are extract from Polarion installer. The process must be performed by each contributor. Please consider to use https://github.com/SchweizerischeBundesbahnen/polarion-artifacts-deployer to extract the dependencies for your own Polarion installer version.

### <a id="design-decisions"></a>Design Decisions
The generic implementation for extensions provided by SBB Polarion Team is located in [ch.sbb.polarion.extension.generic](https://github.com/SchweizerischeBundesbahnen/ch.sbb.polarion.extension.generic)

## <a id="how-can-i-contribute"></a>How Can I Contribute?

### <a id="reporting-bugs"></a>Reporting Bugs
Expand Down
25 changes: 10 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
# PDF-exporter webhook samples
# PDF-Exporter Webhook Samples

This project includes samples of webhooks that can incorporate additional logic for processing HTML prior to PDF generation.
Webhook can be implemented as a REST endpoint by any programming language (Java, JS, Python etc.) and be hosted anywhere (within Polarion as an extension, or absolutely external).
This project contains examples of webhooks that can include additional logic to process HTML prior to PDF generation.
Webhooks can be implemented as a REST endpoint by any programming language (Java, JS, Python, etc.) and hosted anywhere (inside Polarion as an extension or completely external).

## TableCellAutoHeightSetter

The TableCellAutHeightSetter webhook processes HTML content by replacing table cell height values specified in pixels (e.g., height: 100px) with height: auto. The processed HTML is then returned as a response to the client.
The TableCellAutHeightSetter webhook processes HTML content by replacing table cell height values specified in pixels (e.g. height: 100px) with height: auto. The processed HTML is then returned as a response to the client.

## Usage
This webhook endpoint can be started using the following command:
This webhook endpoint can be launched with the following command:

```bash
python app/TableCellAutoHeightSetter.py --port=9333
python table-cell-auto-height-setter/table_cell_auto_height_setter.py --port=9333
```

## Configuration Webhook URL in PDF-exporter:

1. Navigate to: Administration ➙ PDF Export ➙ Webhooks

2. Add the webhook URL, such as http://host.docker.internal:9333/process.




## Configuration of the webhook URL in the PDF Exporter:

1. Navigate to: `Administration``PDF Export``Webhooks`
2. Add the webhook URL, for example http://localhost:9333/td-height/auto.
35 changes: 0 additions & 35 deletions app/TableCellAutoHeightSetter.py

This file was deleted.

57 changes: 57 additions & 0 deletions table-cell-auto-height-setter/table_cell_auto_height_setter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import argparse
import logging
import re
from flask import Flask, Response, request, json
from gevent.pywsgi import WSGIServer

app = Flask(__name__)

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(levelname)s] - %(message)s')


@app.route("/td-height/<height>", methods=["POST"])
def process_html(height):
export_params, html = validate_request()

logging.info(f"Changing HTML table cell height to '{height}'")

if export_params.get('fitToPage'):
html = change_height(html, height)
logging.info("Changing HTML table cell height finished.")
else:
logging.info("'fitToPage' is not set. Changing HTML table cell height skipped.")

return Response(html, mimetype="text/html", status=200)


def change_height(html, height):
pattern = r'(<t[dh].+?height:.*?)(\d+(\.\d+)?px)'
return re.sub(pattern, lambda m: m.group(1) + height, html)


def validate_request():
export_params_json = request.form.get('exportParams')
if export_params_json is None:
return "Missing exportParams", 400
else:
export_params_json = json.loads(export_params_json)

html = request.form.get('html')
if html is None:
return "Missing html", 400
return export_params_json, html


def start_server(port):
http_server = WSGIServer(("", port), app)
http_server.serve_forever()


if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Webhook: table cell auto height setter")
parser.add_argument("--port", default=9333, type=int, required=False, help="Service port")
args = parser.parse_args()

logging.info(f"Service listening on port: {args.port}")

start_server(args.port)

0 comments on commit 069499a

Please sign in to comment.