我有以下PHP脚本导出.doc文件。我想为导出的.doc文件设置特定的页边距,但我没有尝试过。
/*header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; Filename=".rand().".doc");
header("Expires: 0");*/
header( 'Content-Type: application/msword' );
header("Content-disposition: attachment; filename=" .date("Y-m-d").".doc");
/*
header("Content-type: application/vnd.ms-word");
header("Content-disposition: attachment; filename=" .date("Y-m-d").".rtf");
*/
$html = preg_replace('%/[^\\s]+\\.(jpg|jpeg|png|gif)%i', 'http://www.abogaciapractica.com\\0', $_POST['description']);
print "<html xmlns:v=\"urn:schemas-microsoft-com:vml\"";
print "xmlns:o=\"urn:schemas-microsoft-com:office:office\"";
print "xmlns:w=\"urn:schemas-microsoft-com:office:word\"";
print "xmlns=\"http://www.w3.org/TR/REC-html40\">";
print "<xml>
<w:WordDocument>
<w:View>Print</w:View>
<w:DoNotHyphenateCaps/>
<w:PunctuationKerning/>
<w:DrawingGridHorizontalSpacing>9.35 pt</w:DrawingGridHorizontalSpacing>
<w:DrawingGridVerticalSpacing>9.35 pt</w:DrawingGridVerticalSpacing>
</w:WordDocument>
</xml>
";
die($html);
我尝试了以下链接中的代码但没有成功,页面边距保持不变。
http://officeopenxml.com/WPSectionPgMar.php
https://en.wikipedia.org/wiki/Microsoft_Office_XML_formats
提前感谢您的时间。
对于将来的参考,我使用PHPword库找到的替代解决方案更新帖子。
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection(
array('paperSize' => 'Legal', 'marginLeft' => 2834.645669291, 'marginRight' => 1417.322834646, 'marginTop' => 2834.645669291, 'marginBottom' => 1417.322834646)
);
//$html = $_POST['description'];
$html = $_POST['description'];
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);
// Save file
// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
//$objWriter->save('test.docx');
$temp_file_uri = tempnam('', 'xyz');
$objWriter->save($temp_file_uri);
//download code
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=helloWorld.docx');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Content-Length: ' . filesize($temp_file_uri));
readfile($temp_file_uri);
unlink($temp_file_uri); // deletes the temporary file
exit;