douwang6635 2016-10-24 03:56
浏览 1462

PHPWord - 获取页数?

I'm using PHPWord's template parser to make a document, then the command line to auto-print the document. The document MUST be on a single page, because it's a certificate, and is to be printed on special paper.

I'm adding in peoples' names, so people with longer names could throw one line onto two, and push everything down onto a second page.

Does PHPWord have a way to count the number of pages, so I can raise an error if the number of pages exceeds one?

  • 写回答

2条回答 默认 最新

  • duanjie2701 2016-10-24 22:00
    关注

    After pawing through the source, it looks like it can't be done yet.

    However in app.xml there's a tag called "Pages" that should contain the number of pages. I don't think it's accurate, but it's at least something.

    Here's some sample code:

      // Include PHPWord and other stuff before here
      function getPages() {
        $zip = new \PhpOffice\PhpWord\Shared\ZipArchive();
        $zip->open("/path/to/your/document.docx");
        preg_match("/\<Pages>(.*)\<\/Pages\>/", $zip->getFromName("docProps/app.xml"), $var);
        return $var[0];
      }
    

    This returns 1 for me on a document that should have 2 pages. It could be PHPWord not bothering to calculate the number of pages, or it counting page breaks only, but at least it's a start.

    EDIT: Using the Word command line, I can update the page count programatically:

    \path\to\winword.exe /mToolsWordCountRecount /mFileSave /mFileCloseOrExit myfile.docx

    This adds a second or two to generation, but at least now I can accurately detect the number of pages

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?