Generate PDF reports

351 views
Skip to first unread message

saber boukhriss

unread,
Mar 16, 2023, 10:01:29 AM3/16/23
to OWASP ZAP User Group
Hello,
 
I have tried to generate a pdf report using python script, I already tried it on html report, and it worked fine, but when it comes to pdf, it generates a file that can't be opened or is empty.
I tried this at first and found in one of the conversations here that simply changing the saved file extension would work fine (i know it sounds stupid).

# Generate the HTML report
report_html = zap.core.htmlreport()

# Save the report to a file
with open('report.pdf', 'w') as f:
    f.write(report_html)


Then I tried this from what I understood from the documentation.

# URL of the ZAP API endpoint

# Parameters to include in the API request
params = {
    'apikey': apiKey,
    'filename': 'report.pdf'
}

# Make the API request to generate the report
response = requests.get(url, params=params)

# Write the response content to a file
with open('report.pdf', 'wb') as f:
    f.write(response.content)

Do you have any help or suggestions?

Simon Bennetts

unread,
Mar 16, 2023, 10:03:58 AM3/16/23
to OWASP ZAP User Group

saber boukhriss

unread,
Mar 16, 2023, 10:08:00 AM3/16/23
to OWASP ZAP User Group
Thank you Simon for answering. Do you have any article or a video that I can follow?

Simon Bennetts

unread,
Mar 16, 2023, 10:10:03 AM3/16/23
to OWASP ZAP User Group
All of the ZAP videos are on https://www.zaproxy.org/videos-list/

If you search in the title for "Report" you should find one :)

saber boukhriss

unread,
Mar 16, 2023, 10:21:33 AM3/16/23
to OWASP ZAP User Group
There's only one video, and it's about using UI. As I mentioned earlier, I'm using daemon owasp and executing queries through python scripts.

thc...@gmail.com

unread,
Mar 16, 2023, 10:30:56 AM3/16/23
to zaprox...@googlegroups.com
See:
https://www.zaproxy.org/docs/desktop/addons/report-generation/api/

Best regards.

On 16/03/2023 14:21, saber boukhriss wrote:
> There's only one video <https://www.youtube.com/watch?v=kD540gUWJ3I>, and

Muhammad Zubair

unread,
Mar 16, 2023, 11:27:50 AM3/16/23
to OWASP ZAP User Group
Hi Here is Solution for you,
One possible reason for the generated PDF file being empty or unable to open could be the encoding of the HTML report. When you open a file in text mode with open(), Python uses the default encoding which may not be suitable for writing binary data to a file. To fix this issue, you can open the file in binary mode by using 'wb' instead of 'w'. Here's an updated version of the code that should generate a valid PDF report:
import io
import pdfkit
import requests
import io
import pdfkit
import requests


# Generate the HTML report
report_html = zap.core.htmlreport()

# Convert the HTML report to a PDF using pdfkit
pdf = pdfkit.from_string(report_html, False)

# Save the PDF report to a file
with open('report.pdf', 'wb') as f:
    f.write(pdf)


# URL of the ZAP API endpoint
url = 'http://localhost:8080/OTHER/core/other/htmlreport/'

# Parameters to include in the API request
params = {
    'apikey': apiKey,
    'filename': 'report.pdf'
}

# Make the API request to generate the report
response = requests.get(url, params=params)

# Write the response content to a file
with open('report.pdf', 'wb') as f:
    f.write(response.content)

This code uses the pdfkit library to convert the HTML report to a PDF, and then saves the PDF file to disk using binary mode. It should work for most cases, but make sure to install the pdfkit library using pip or other package manager before running the code.

saber boukhriss

unread,
Mar 17, 2023, 5:01:32 AM3/17/23
to OWASP ZAP User Group
still the same error :/

saber boukhriss

unread,
Mar 17, 2023, 5:44:40 AM3/17/23
to OWASP ZAP User Group
I solved it by adding this code :D 
from io import BytesIO
from xhtml2pdf import pisa

# Generate the HTML report
report_html = zap.core.htmlreport()

# Convert HTML to PDF
pdf = BytesIO()
pisa.CreatePDF(BytesIO(report_html.encode('utf-8')), pdf)

# Save the PDF report to a file
with open('report.pdf', 'wb') as f:
    f.write(pdf.getvalue())

Chathuranga

unread,
May 14, 2024, 6:56:17 AM5/14/24
to ZAP User Group
import os
from bs4 import BeautifulSoup
from weasyprint import HTML, CSS

def html_to_pdf(html_file, output_pdf):
    with open(html_file, 'r', encoding='utf-8') as f:
        html_content = f.read()

    # Parse HTML
    soup = BeautifulSoup(html_content, 'html.parser')

    # Get all CSS files
    css_files = []
    for link in soup.find_all('link', rel='stylesheet'):
        css_files.append(link.get('href'))

    # Load HTML and CSS
    html = HTML(string=html_content)
    css_content_list = []
    for css_file in css_files:
        with open(css_file, 'r', encoding='utf-8') as f:
            css_content = f.read()
            css_content_list.append(css_content)

    # Write PDF
    css = CSS(string='\n'.join(css_content_list))
    html.write_pdf(output_pdf, stylesheets=[css])
    print("PDF created successfully.")

if __name__ == "__main__":
    html_file = input("Enter the HTML file name: ")
    output_pdf = "output.pdf"
   
    html_to_pdf(html_file, output_pdf)




use this script
Reply all
Reply to author
Forward
0 new messages