Generate PDF from .docx generated by PHPWord

前端 未结 4 1332
梦如初夏
梦如初夏 2021-01-01 21:07

I am creating .docx files from a template using PHPWord. It works fine but now I want to convert the generated file to PDF.

Fi

相关标签:
4条回答
  • 2021-01-01 21:37

    Try this:

    // get the name of the input PDF
    $inputFile = "C:\\PHP\\Test1.docx";
    
    // get the name of the output MS-WORD file
    $outputFile = "C:\\PHP\\Test1.pdf";
    
    try
        {
        $oLoader = new COM("easyPDF.Loader.8");
        $oPrinter = $oLoader->LoadObject("easyPDF.Printer.8");
        $oPrintJob = $oPrinter->PrintJob;
        $oPrintJob->PrintOut ($inputFile, $outputFile);
        print "Success";
        }
    
    
    catch(com_exception $e)
        {
        Print "error code".$e->getcode(). "\n";
        print $e->getMessage();
        }
    
    0 讨论(0)
  • 2021-01-01 21:40

    I used Gears/pdf to convert the docx file generated by phpword to PDF:

    $success = Gears\Pdf::convert(
                'file_path/file_name.docx',
                'file_path/file_name.pdf');
    
    0 讨论(0)
  • 2021-01-01 21:46

    I don't think I'm correct.. You save the document as HTML content

    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');

    After than you read the HTML file content and write the content as PDF file with the help of mPDF or tcPdf or fpdf.

    0 讨论(0)
  • 2021-01-01 21:50

    You're trying to unlink the PDF file before saving it, and you have also to unlink the DOCX document, not the PDF one.

    Try this.

    $pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordPdf , 'PDF');    
    $pdfWriter->save($filename.".pdf");
    unlink($wordPdf);
    
    0 讨论(0)
提交回复
热议问题