from odoo.tests.common import TransactionCase
from odoo.tools.misc import find_in_path
class TestHtmlToPdf(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
# Check if wkhtmltopdf is available
cls.wkhtmltopdf_available = bool(find_in_path("wkhtmltopdf"))
# Get the model class to access the classmethod
cls.account_move = cls.env["account.move"]
# Simple test HTML content
cls.test_html = """
Test HTML Document
This is a test paragraph with bold text and italic text.
- List item 1
- List item 2
- List item 3
"""
def test_html_to_pdf_conversion(self):
"""Test the direct HTML to PDF conversion."""
if not self.wkhtmltopdf_available:
self.skipTest("wkhtmltopdf not available")
# Call the method to convert HTML to PDF
pdf_content = self.account_move._html_to_pdf(self.test_html)
# Verify the PDF was created
self.assertTrue(pdf_content, "PDF content should be generated")
# Verify it's a valid PDF
self.assertTrue(
pdf_content.startswith(b"%PDF-"), "Content should be a valid PDF"
)
self.assertTrue(len(pdf_content) > 100, "PDF should have reasonable size")
def test_html_to_pdf_with_complex_content(self):
"""Test HTML to PDF conversion with more complex content."""
if not self.wkhtmltopdf_available:
self.skipTest("wkhtmltopdf not available")
# More complex HTML with tables and images
complex_html = """
Complex HTML Test
| Header 1 |
Header 2 |
Header 3 |
| Row 1, Cell 1 |
Row 1, Cell 2 |
Row 1, Cell 3 |
| Row 2, Cell 1 |
Row 2, Cell 2 |
Row 2, Cell 3 |
"""
# Convert complex HTML to PDF
pdf_content = self.account_move._html_to_pdf(complex_html)
# Verify the PDF was created
self.assertTrue(
pdf_content, "PDF content should be generated from complex HTML"
)
self.assertTrue(
pdf_content.startswith(b"%PDF-"), "Content should be a valid PDF"
)
self.assertTrue(len(pdf_content) > 100, "PDF should have reasonable size")