Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

ms word - phpword total pages not printing correctly from browser download

I am creating a phpword document that should have a "Page 3 of 8" style footer on each page. The document generates correctly on the server. When it opens in MS Word, the page numbers show up correctly on my computer screen. When I open print preview in Word, everything looks correct. When I send it to the printer, that's when it screws up. The physical pages out of my printer say Page 1 of 1, Page 2 of 2, Page 3 of 3, and so on. This happens whether I send it to a real printer or if I send it to a virtual PDF printer.

It appears that the problem is HOW the file gets opened.

My code is posted below. The last line of code creates a clickable link that will download the word file from the server into my local machine's downloads folder and my browser then gives me the option to open the file in MS Word from a button at the bottom of the screen. I don't know how or why but this series of actions is what's causing the problem.

When I open the file in this manner, MS Word first opens in Protected View. When you enable editing it goes into Compatibility Mode. If I print the file from there, I get the problem described above.

If I save the file to somewhere else on my computer I am given a prompt that I am saving the file to a new format and it discusses compatibility mode. Then, if I close the file and reopen it with the updated format, it prints fine.

If I open the file directly from my downloads folder instead of using the browser button, the same issues exists.

If I navigate to my server folder and download the word document from my server onto my machine and then open it, the file prints correctly even though it says that it is still in compatibility mode.

I have trouble-shot everything I can think of. Why does downloading the file through a browser have any effect on why the total pages would not print correctly?

<?php

//Starting WORD Document
//Calling necessary support file
require_once("vendor/autoload.php"); 
//Creating a new document
$phpWord = new PhpOfficePhpWordPhpWord();

$section = $phpWord->addSection();

$footer = $section->addFooter();
$footer->addPreserveText('Page {PAGE} of {NUMPAGES}');

//Start Text of Document
$section->addText("SomeText");
$section->addPageBreak();
$section->addText("SomeText");
$section->addPageBreak();
$section->addText("SomeText");
$section->addPageBreak();
$section->addText("SomeText");
$section->addPageBreak();
$section->addText("SomeText");
$section->addPageBreak();


// Saving the document as OOXML file...
$objWriter = PhpOfficePhpWordIOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('PageNumbersTest.docx');

$time = date("h:i:sa");
$file = "PageNumbersTest.docx?=";
$url = $file . $time;

echo '<a href='.$url.'>Click here</a>';

?>
question from:https://stackoverflow.com/questions/65928786/phpword-total-pages-not-printing-correctly-from-browser-download

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...