一、html 转为pdf 文件
安装第三方类
composer require dompdf/dompdf
加载字体文件
将以下代码命名为 load_font.php 放在项目根目录,加载字体的目的是为了在生成中文时不出现乱码
<?php
require_once "vendor/autoload.php";
use Dompdf\Dompdf;
use Dompdf\CanvasFactory;
use Dompdf\Exception;
use Dompdf\FontMetrics;
use Dompdf\Options;
use FontLib\Font;
function usage() {
echo <<<EOD
Usage: {$_SERVER["argv"][0]} font_family [n_file [b_file] [i_file] [bi_file]]
font_family: the name of the font, e.g. Verdana, 'Times New Roman',
monospace, sans-serif. If it equals to "system_fonts",
all the system fonts will be installed.
n_file: the .ttf or .otf file for the normal, non-bold, non-italic
face of the font.
{b|i|bi}_file: the files for each of the respective (bold, italic,
bold-italic) faces.
If the optional b|i|bi files are not specified, load_font.php will search
the directory containing normal font file (n_file) for additional files that
it thinks might be the correct ones (e.g. that end in _Bold or b or B). If
it finds the files they will also be processed. All files will be
automatically copied to the DOMPDF font directory, and afm files will be
generated using php-font-lib (https://github.com/PhenX/php-font-lib).
Examples:
./load_font.php silkscreen /usr/share/fonts/truetype/slkscr.ttf
./load_font.php 'Times New Roman' /mnt/c_drive/WINDOWS/Fonts/times.ttf
EOD;
exit;
}
if ( $_SERVER["argc"] < 3 && @$_SERVER["argv"][1] != "system_fonts" ) {
usage();
}
$dompdf = new Dompdf();
if (isset($fontDir) && realpath($fontDir) !== false) {
$dompdf->getOptions()->set('fontDir', $fontDir);
}
/**
* Installs a new font family
* This function maps a font-family name to a font. It tries to locate the
* bold, italic, and bold italic versions of the font as well. Once the
* files are located, ttf versions of the font are copied to the fonts
* directory. Changes to the font lookup table are saved to the cache.
*
* @param Dompdf $dompdf dompdf main object
* @param string $fontname the font-family name
* @param string $normal the filename of the normal face font subtype
* @param string $bold the filename of the bold face font subtype
* @param string $italic the filename of the italic face font subtype
* @param string $bold_italic the filename of the bold italic face font subtype
*
* @throws Exception
*/
function install_font_family($dompdf, $fontname, $normal, $bold = null, $italic = null, $bold_italic = null) {
$fontMetrics = $dompdf->getFontMetrics();
if ( !is_readable($normal) )
throw new Exception("Unable to read '$normal'.");
$dir = dirname($normal);
$basename = basename($normal);
$last_dot = strrpos($basename, '.');
if ($last_dot !== false) {
$file = substr($basename, 0, $last_dot);
$ext = strtolower(substr($basename, $last_dot));
} else {
$file = $basename;
$ext = '';
}
if ( !in_array($ext, array(".ttf", ".otf")) ) {
throw new Exception("Unable to process fonts of type '$ext'.");
}
$path = "$dir/$file";
$patterns = array(
"bold" => array("_Bold", "b", "B", "bd", "BD"),
"italic" => array("_Italic", "i", "I"),
"bold_italic" => array("_Bold_Italic", "bi", "BI", "ib", "IB"),
);
foreach ($patterns as $type => $_patterns) {
if ( !isset($$type) || !is_readable($$type) ) {
foreach($_patterns as $_pattern) {
if ( is_readable("$path$_pattern$ext") ) {
$$type = "$path$_pattern$ext";
break;
}
}
if ( is_null($$type) )
echo ("Unable to find $type face file.\n");
}
}
$fonts = compact("normal", "bold", "italic", "bold_italic");
$entry = array();
foreach ($fonts as $var => $src) {
if ( is_null($src) ) {
$entry[$var] = $dompdf->getOptions()->get('fontDir') . '/' . mb_substr(basename($normal), 0, -4);
continue;
}
if ( !is_readable($src) )
throw new Exception("Requested font '$src' is not readable");
$dest = $dompdf->getOptions()->get('fontDir') . '/' . basename($src);
if ( !is_writeable(dirname($dest)) )
throw new Exception("Unable to write to destination '$dest'.");
echo "Copying $src to $dest...\n";
if ( !copy($src, $dest) )
throw new Exception("Unable to copy '$src' to '$dest'");
$entry_name = mb_substr($dest, 0, -4);
echo "Generating Adobe Font Metrics for $entry_name...\n";
$font_obj = Font::load($dest);
$font_obj->saveAdobeFontMetrics("$entry_name.ufm");
$font_obj->close();
$entry[$var] = $entry_name;
}
$fontMetrics->setFontFamily($fontname, $entry);
$fontMetrics->saveFontFamilies();
}
if ( $_SERVER["argv"][1] === "system_fonts" ) {
$fontMetrics = $dompdf->getFontMetrics();
$files = glob("/usr/share/fonts/truetype/*.ttf") +
glob("/usr/share/fonts/truetype/*/*.ttf") +
glob("/usr/share/fonts/truetype/*/*/*.ttf") +
glob("C:\\Windows\\fonts\\*.ttf") +
glob("C:\\WinNT\\fonts\\*.ttf") +
glob("/mnt/c_drive/WINDOWS/Fonts/");
$fonts = array();
foreach ($files as $file) {
$font = Font::load($file);
$records = $font->getData("name", "records");
$type = $fontMetrics->getType($records[2]);
$fonts[mb_strtolower($records[1])][$type] = $file;
$font->close();
}
foreach ( $fonts as $family => $files ) {
echo " >> Installing '$family'... \n";
if ( !isset($files["normal"]) ) {
echo "No 'normal' style font file\n";
}
else {
install_font_family($dompdf, $family, @$files["normal"], @$files["bold"], @$files["italic"], @$files["bold_italic"]);
echo "Done !\n";
}
echo "\n";
}
}
else {
call_user_func_array("install_font_family", array_merge( array($dompdf), array_slice($_SERVER["argv"], 1) ));
}
将自己使用的ttf格式的字体放在 public/fonts/ 目录下,这里使用 simsun.ttf 做示例
导入字体
在项目根目录执行命令
php load_font.php public/fonts/simsun.ttf
成功后在 vendor/dompdf/dompdf/lib/fonts/ 下会出现导入的字体
html转pdf核心代码
$css = '<style>
body {
word-wrap: break-word;
word-break: break-all;
font-size: 10px !important;
}
img{
max-width:100%;
}
</style>';
//css样式是控制文字换行和大小以及图片展示
$html = '<html><head></head><body><p>你好</p></body></html>';
$html = substr_replace($html, $css, strpos($html, '</head>'), 0);
$options = new Options();
$options->setIsRemoteEnabled(true);//是否允许加载远程资源(如图片)。
$options->setDefaultFont('simsun');//设置字体
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);//导入html内容
$dompdf->setPaper('A4');//设置pdf每页大小
$dompdf->render();
// file_put_contents('document.pdf', $dompdf->output());//保存本地文件
$dompdf->stream('document.pdf', ['Attachment' => false]);//输出到浏览器
exit;
至此,html转pdf项目基本完成,