0

I'm currently trying to add HTML content to an existing PDF document using the FPDI library to import the PDF and the mPDF library to generate the HTML content in my wordpress project. However, I'm facing difficulties as my code doesn't seem to display anything in the resulting PDF.

Problem:

I'm using the FPDI library to import an existing PDF document. The import process seems to work fine, but when I try to add HTML content using mPDF, it doesn't appear in the output PDF.

I'm generating HTML content using the mPDF library and then attempting to add this content to the imported PDF pages using the FPDI library. However, the generated HTML doesn't display as expected in the resulting PDF. I've double-checked my code, but I can't identify the issue.

Code:

Here's a simplified version of my code:

// ... (include necessary libraries)

function generate_pdf_from_url_param() { // ... (handle GET parameters and other setup)

// Create FPDI object and import existing PDF
$fpdi = new FpdiLibrary();
$pageCount = $fpdi->setSourceFile(get_template_directory() . '/existing.pdf');

// Loop through pages and add content
for ($page = 1; $page <= $pageCount; $page++) {
    $tplIdx = $fpdi->importPage($page);
    $fpdi->AddPage();
    $fpdi->useTemplate($tplIdx);
    
    // Create mPDF object and generate HTML content
    $mpdf = new MpdfLibrary();
    // ... (generate HTML content)
    
    // Get the HTML content from mPDF
    $html_content = $mpdf->Output('', 'S'); // Captures the output as a string
    
    // Add HTML content to PDF using FPDI
    // This line does not seem to work as expected
    $fpdi->SetXY(30, 190); // Set position for HTML content
    $fpdi->MultiCell(0, 10, $html_content);
}

// ... (other steps and output)

} add_action('init', 'generate_pdf_from_url_param');

I'm not sure why the HTML content generated using mPDF is not displaying in the output PDF. Could someone please provide guidance on how to properly integrate HTML content into an existing PDF using FPDI and mPDF? If there's a better approach to achieve this, I'd greatly appreciate your insights.

Thank you for your assistance!

  • we can do it using MPDF where we can add HTML in to it – user2492580 Aug 22 '23 at 11:38
  • but now the problem is that fpdi is not accepting the HTML code generated by mpdf. – user2492580 Aug 22 '23 at 11:42
  • actually what i want is a graph which i want to render on the pdf, it is generated after a test and i have a 15 page repost. i want the graphs to be displayed on the 1st page keeping rest of the pages as it is – user2492580 Aug 22 '23 at 12:37
  • 1
    Based on your comment, it sounds like you have a template PDF, and you want to add a page to the PDF that is rendered from HTML content. In my experience, the best way to do this is to generate a PDF file from the HTML content, then inject it into the template PDF and save the result to a new PDF file. I use WKHTMOTOPDF to generate PDFs from HTML (you could use mPDF for this), then PDFBox to merge the generated PDF and the template. You need to shell out to do this (or use PHP-Java bridge), and depending on how deep you want to go, maybe write some JAVA. – Rob Ruchte Aug 22 '23 at 16:34

0 Answers0