的PHP
这段代码生成的PDF文件应该可以在自动收录机上完美打印。如果要在监视器上查看PDF文件,则可能需要放大一点。
原始文件范例
PDF输出(在浏览器中查看)
源代码
<?php
header("Content-Type: application/pdf");
$s = docx2txt("word-file.docx"); // <-- Insert filename here!
echo txt2pdf($s);
function docx2txt($filename) {
if (!($z=zip_open($filename))) return false; // Can't open file
while ($r=zip_read($z)) {
if (zip_entry_name($r)!="word/document.xml") continue;
if (!zip_entry_open($z,$r)) return false; // Can't open XML data
for ($s="";;) {
$c=zip_entry_read($r);
if ($c===false || $c=="") break;
$s.=$c;
}
return trim(preg_replace('/\s+/',' ',preg_replace('/<[^>]*>/','',$s)));
}
return false; // Can't find XML data
}
function txt2pdf($text) {
$width="".ceil(strlen($text)*7.2);
$text=str_replace('(','\050',str_replace(')','\051',$text));
$length=strlen($text);
$wlen=strlen($width);
$len4="".(44+$length);
$xr3=sprintf("%010d",174+$wlen);
$xr4=sprintf("%010d",449+$wlen);
$xrstart=544+$wlen+strlen($len4)+$length;
return "%PDF-1.1\n%¥±ë\n\n1 0 obj\n << /Type /Catalog\n /Pages 2 0 R\n" .
" >>\nendobj\n\n2 0 obj\n << /Type /Pages\n /Kids [3 0 R]\n " .
" /Count 1\n /MediaBox [0 0 $width 14]\n >>\nendobj\n\n3 0 obj" .
"\n << /Type /Page\n /Parent 2 0 R\n /Resources\n " .
"<< /Font\n << /F1\n << /Type /Font\n " .
" /Subtype /Type1\n /BaseFont /Courier\n " .
" >>\n >>\n >>\n /Contents 4 0 R\n" .
" >>\nendobj\n\n4 0 obj\n << /Length $len4 >>\nstream\n BT\n /" .
"F1 12 Tf\n 0 3 Td\n ($text) Tj\n ET\nendstream\nendobj\n\nxr" .
"ef\n0 5\n0000000000 65535 f \n0000000018 00000 n \n0000000077 00000" .
" n \n$xr3 00000 n \n$xr4 00000 n \ntrailer\n << /Root 1 0 R\n " .
" /Size 5\n >>\nstartxref\n$xrstart\n%%EOF";
}
?>
注意:该txt2pdf()
功能基于Brendan Zagaeski制作的最小PDF文件。