From 1a1b0c85267e5bdd3fe2d431ddb4a2ff641be4cd Mon Sep 17 00:00:00 2001 From: llaszuk-r7 <99184394+llaszuk-r7@users.noreply.github.com> Date: Wed, 22 Nov 2023 13:00:21 +0100 Subject: [PATCH] soar-15967 change library for generating pdf (#2122) (#2133) --- plugins/pdf_generator/.CHECKSUM | 6 ++--- plugins/pdf_generator/Dockerfile | 3 +-- .../pdf_generator/bin/komand_pdf_generator | 2 +- plugins/pdf_generator/help.md | 5 ++-- .../actions/generate_pdf/action.py | 27 +++++-------------- plugins/pdf_generator/plugin.spec.yaml | 4 +-- plugins/pdf_generator/requirements.txt | 2 +- plugins/pdf_generator/setup.py | 2 +- 8 files changed, 19 insertions(+), 32 deletions(-) diff --git a/plugins/pdf_generator/.CHECKSUM b/plugins/pdf_generator/.CHECKSUM index e2af116ac0..5328fe7592 100644 --- a/plugins/pdf_generator/.CHECKSUM +++ b/plugins/pdf_generator/.CHECKSUM @@ -1,7 +1,7 @@ { - "spec": "ef0d3ba33cfdb35d007174f323079e63", - "manifest": "2daa5f5af570451e47c6d6c91e425794", - "setup": "2a984e9284f0d9571da1a489fb3e59ea", + "spec": "7ba32756f9b484b9b79ffd05783e0859", + "manifest": "01dda17cbcc14f85d4868b3614150adc", + "setup": "97280325f9cb949dbf62fbcf21fc973e", "schemas": [ { "identifier": "generate_pdf/schema.py", diff --git a/plugins/pdf_generator/Dockerfile b/plugins/pdf_generator/Dockerfile index a484cacfd2..0cb9dd97d1 100644 --- a/plugins/pdf_generator/Dockerfile +++ b/plugins/pdf_generator/Dockerfile @@ -1,4 +1,4 @@ -FROM rapid7/insightconnect-python-3-38-plugin:5 +FROM rapid7/insightconnect-python-3-plugin:5 LABEL organization=rapid7 LABEL sdk=python @@ -8,7 +8,6 @@ WORKDIR /python/src ADD ./plugin.spec.yaml /plugin.spec.yaml ADD ./requirements.txt /python/src/requirements.txt -RUN apt-get update && apt-get install -y python3-pip python3-cffi python3-brotli libpango-1.0-0 libpangoft2-1.0-0 libpangocairo-1.0-0 RUN if [ -f requirements.txt ]; then pip install -r requirements.txt; fi ADD . /python/src diff --git a/plugins/pdf_generator/bin/komand_pdf_generator b/plugins/pdf_generator/bin/komand_pdf_generator index e9d47e558f..dfd50aa2cf 100755 --- a/plugins/pdf_generator/bin/komand_pdf_generator +++ b/plugins/pdf_generator/bin/komand_pdf_generator @@ -6,7 +6,7 @@ from sys import argv Name = "PDF Generator" Vendor = "rapid7" -Version = "1.0.2" +Version = "1.0.3" Description = "Create PDF reports from workflow data" diff --git a/plugins/pdf_generator/help.md b/plugins/pdf_generator/help.md index 4625cb09c6..4c3d208d3c 100644 --- a/plugins/pdf_generator/help.md +++ b/plugins/pdf_generator/help.md @@ -14,7 +14,7 @@ reports to distribute workflow details. # Supported Product Versions * Pillow 10.1.0 -* WeasyPrint==52.5 +* fpdf2==2.7.6 # Documentation @@ -75,13 +75,14 @@ Example output: # Version History +* 1.0.3 - Change library to generate PDF * 1.0.2 - Update plugin runtime to InsightConnect | Add unit tests | Updated all dependencies to the newest versions * 1.0.1 - New spec and help.md format for the Extension Library * 1.0.0 - Initial plugin # Links -[Weasyprint](https://doc.courtbouillon.org/weasyprint/stable/) +[PyPDF2](https://pypdf2.readthedocs.io/) ## References diff --git a/plugins/pdf_generator/komand_pdf_generator/actions/generate_pdf/action.py b/plugins/pdf_generator/komand_pdf_generator/actions/generate_pdf/action.py index 90f6c9087f..39452de86e 100755 --- a/plugins/pdf_generator/komand_pdf_generator/actions/generate_pdf/action.py +++ b/plugins/pdf_generator/komand_pdf_generator/actions/generate_pdf/action.py @@ -1,9 +1,7 @@ from base64 import b64encode -from html import escape -from textwrap import wrap import insightconnect_plugin_runtime -from weasyprint import HTML +from fpdf import FPDF from .schema import GeneratePdfInput, GeneratePdfOutput @@ -20,22 +18,11 @@ def __init__(self): def run(self, params={}): text = params.get("text") - html_template = """ - - - -
{}
-""" - # Wrap text preserving existing newlines - text = "\n".join( - wrapped - for line in text.splitlines() - for wrapped in wrap(line, width=70, expand_tabs=False, replace_whitespace=False, drop_whitespace=False) - ) - text = escape(text) - html_content = html_template.format(text) - pdf_content = HTML(string=html_content).write_pdf() - - b64_content = b64encode(pdf_content).decode() + pdf = FPDF(format="A4") + pdf.add_page() + pdf.set_font("helvetica", size=12) + pdf.multi_cell(0, text=text) + pdf_bytes = pdf.output() + b64_content = b64encode(pdf_bytes).decode() return {"pdf": b64_content} diff --git a/plugins/pdf_generator/plugin.spec.yaml b/plugins/pdf_generator/plugin.spec.yaml index 9956aa8d0d..aef7d1ab16 100644 --- a/plugins/pdf_generator/plugin.spec.yaml +++ b/plugins/pdf_generator/plugin.spec.yaml @@ -4,8 +4,8 @@ products: [insightconnect] name: pdf_generator title: PDF Generator description: Create PDF reports from workflow data -version: 1.0.2 -supported_versions: ["Pillow 10.1.0", "WeasyPrint==52.5"] +version: 1.0.3 +supported_versions: ["Pillow 10.1.0", "fpdf2==2.7.6"] vendor: rapid7 support: community sdk: diff --git a/plugins/pdf_generator/requirements.txt b/plugins/pdf_generator/requirements.txt index a18df6c9f5..36ceec7c1e 100755 --- a/plugins/pdf_generator/requirements.txt +++ b/plugins/pdf_generator/requirements.txt @@ -1,5 +1,5 @@ # List third-party dependencies here, separated by newlines. # All dependencies must be version-pinned, eg. requests==1.2.0 # See: https://pip.pypa.io/en/stable/user_guide/#requirements-files +fpdf2==2.7.6 Pillow==10.1.0 -WeasyPrint==52.5 diff --git a/plugins/pdf_generator/setup.py b/plugins/pdf_generator/setup.py index 6196ec6ae3..92e5a0d476 100644 --- a/plugins/pdf_generator/setup.py +++ b/plugins/pdf_generator/setup.py @@ -3,7 +3,7 @@ setup(name="pdf_generator-rapid7-plugin", - version="1.0.2", + version="1.0.3", description="Create PDF reports from workflow data", author="rapid7", author_email="",