Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DomPDF not working with TemplateProcessor docs, I Try to convert template docs to pdf. #2568

Closed
BahaaEldeenOsama opened this issue Feb 12, 2024 · 1 comment

Comments

@BahaaEldeenOsama
Copy link

Describe the Bug

TemplateProcessor not working with IOFactory::createWriter().

PhpOffice\PhpWord\IOFactory::createWriter(): Argument 1 ($phpWord) must be of type PhpOffice\PhpWord\PhpWord, PhpOffice\PhpWord\TemplateProcessor

Steps to Reproduce

Please provide a code sample that reproduces the issue.

<?php
  $templateProcessor = new TemplateProcessor('temp.docx');
  $FullFileName  = 'NewTemp.pdf' ;
    
  // Save the document to a temporary file
  $filename = tempnam(sys_get_temp_dir(), 'NewTemp' ) . '.docx';
  $templateProcessor->saveAs($filename);
  
  // Convert Word document to HTML
  $htmlWriter = IOFactory::createWriter($templateProcessor, 'HTML');
  $htmlContent = $htmlWriter->saveToString();

  // Initialize DomPDF
  $options = new Options();
  $options->set('isHtml5ParserEnabled', true);
  $options->set('isPhpEnabled', true);
  $dompdf = new Dompdf($options);

  // Load HTML content into DomPDF
  $dompdf->loadHtml($htmlContent);

  // Set paper size (optional)
  $dompdf->setPaper('A4', 'portrait');

  // Render PDF (output)
  $dompdf->render();

  // Set headers for a PDF download
  header('Content-Type: application/pdf');
  header('Content-Disposition: attachment; filename='.$FullFileName);

  // Output the PDF content to the browser
  echo $dompdf->output();

  // Delete the temporary Word document
  unlink($wordFilePath);     

Context

Please fill in your environment information:

  • PHP Version: "^8.1"
  • PHPWord Version: "^1.2.0"
  • Mpdf Version : "^8.2"
  • Tecnickcom/tcpdf : "^6.2.13"
@oleibman
Copy link
Contributor

I don't think the template processor was designed to work with anything other than Word2007 format. However, since you've saved the output file, you should be able to read it back in:

  // Convert Word document to HTML
  $reader = new \PhpOffice\PhpWord\Reader\Word2007();
  $phpWord = $reader->load($filename);
  $htmlWriter = new \PhpOffice\PhpWord\Writer\HTML($phpWord)
  $htmlContent = $htmlWriter->saveToString();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants