mcs/html-to-pdf

该包已被废弃且不再维护。未建议替代包。

将HTML字符串转换为PDF字符串

0.0.1 2015-10-17 19:19 UTC

This package is not auto-updated.

Last update: 2024-04-15 18:24:09 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

安装

$ composer require mcs/html-to-pdf

特性

  • 使用wkhtmltopdf将HTML字符串转换为PDF字符串
  • 不使用临时文件

基本用法

try{

    $pdf = new MCS\HtmlToPdf('<html><body>Hi!<body></html>', '/usr/local/bin/wkhtmltopdf');

    header('Content-Type: application/pdf');

    echo $pdf->generate();

}
catch(Exception $e){
    echo $e->getMessage();
}

高级用法

try{

    //Get your html string
    $html = file_get_contents('demo.html');

    // Path to wkhtmltopdf
    $pathToWkhtmltopdf = '/usr/local/bin/wkhtmltopdf';

    // Initialise
    $pdf = new MCS\HtmlToPdf($html, $pathToWkhtmltopdf);

    // Add a command before the wkhtmltopdf command
    $pdf->addBeforeCommand('unset DYLD_LIBRARY_PATH');

    // Add wkhtmltopdf parameters, second parameter is optional. See: http://wkhtmltopdf.org/usage/wkhtmltopdf.txt
    $pdf->setParam('grayscale');
    $pdf->setParam('orientation', 'landscape');

    // Execute
    $pdfString = $pdf->generate();

    // Show the generated pdf
    header('Content-Type: application/pdf');
    die($pdfString);

}
catch(Exception $e){
    echo $e->getMessage();
}