Skip to content

Commit

Permalink
soar-15967 change library for generating pdf (#2122) (#2133)
Browse files Browse the repository at this point in the history
  • Loading branch information
llaszuk-r7 authored Nov 22, 2023
1 parent b87fdc5 commit 1a1b0c8
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 32 deletions.
6 changes: 3 additions & 3 deletions plugins/pdf_generator/.CHECKSUM
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"spec": "ef0d3ba33cfdb35d007174f323079e63",
"manifest": "2daa5f5af570451e47c6d6c91e425794",
"setup": "2a984e9284f0d9571da1a489fb3e59ea",
"spec": "7ba32756f9b484b9b79ffd05783e0859",
"manifest": "01dda17cbcc14f85d4868b3614150adc",
"setup": "97280325f9cb949dbf62fbcf21fc973e",
"schemas": [
{
"identifier": "generate_pdf/schema.py",
Expand Down
3 changes: 1 addition & 2 deletions plugins/pdf_generator/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion plugins/pdf_generator/bin/komand_pdf_generator
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down
5 changes: 3 additions & 2 deletions plugins/pdf_generator/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ reports to distribute workflow details.
# Supported Product Versions

* Pillow 10.1.0
* WeasyPrint==52.5
* fpdf2==2.7.6

# Documentation

Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -20,22 +18,11 @@ def __init__(self):
def run(self, params={}):
text = params.get("text")

html_template = """
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title></title></head>
<body><pre>{}</pre></body>
</html>"""
# 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}
4 changes: 2 additions & 2 deletions plugins/pdf_generator/plugin.spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion plugins/pdf_generator/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion plugins/pdf_generator/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="",
Expand Down

0 comments on commit 1a1b0c8

Please sign in to comment.