PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.25k stars 2.69k forks source link

Problem: read header and footer in documents #293

Open hp3k opened 10 years ago

hp3k commented 10 years ago

How to get header and footer in documents and parse to html?

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/2832508-problem-read-header-and-footer-in-documents?utm_campaign=plugin&utm_content=tracker%2F323108&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F323108&utm_medium=issues&utm_source=github).
hp3k commented 10 years ago

I have a PHP code:

<?php
if(isset($_FILES['document'])){
    include 'fileupload.php';
    $source = 'upload/'.$_FILES['document']['name'];
    echo date('H:i:s'), " Reading contents from `{$source}`", EOL;
    $phpWord = \PhpOffice\PhpWord\IOFactory::load($source);
    print_r($phpWord);

}

1) I have exception in docx document: uploaded12:33:19 Reading contents from upload/generate_file.docx

( ! ) Fatal error: Uncaught exception 'PhpOffice\PhpWord\Exception\InvalidImageException' in C:\wamp\www\src\PhpWord\Element\Image.php on line 371

( ! ) PhpOffice\PhpWord\Exception\InvalidImageException: in C:\wamp\www\src\PhpWord\Element\Image.php on line 371

Call Stack

#

Time

Memory

Function

Location

1 0.0006 248248 {main}( ) ..\Sample_32_Coig.php:0 2 0.0058 347992 PhpOffice\PhpWord\IOFactory::load( ) ..\Sample_32_Coig.php:53 3 0.0072 404328 PhpOffice\PhpWord\Reader\Word2007->load( ) ..\IOFactory.php:73 4 0.0293 1118552 PhpOffice\PhpWord\Reader\Word2007->readPart( ) ..\Word2007.php:69 5 0.0300 1168512 PhpOffice\PhpWord\Reader\Word2007\Document->read( ) ..\Word2007.php:93 6 0.0402 1632888 PhpOffice\PhpWord\Reader\Word2007\Document->readWPNode( ) ..\Document.php:57 7 0.0402 1632888 PhpOffice\PhpWord\Reader\Word2007\AbstractPart->readParagraph( ) ..\Document.php:157 8 0.0412 1634360 PhpOffice\PhpWord\Reader\Word2007\AbstractPart->readRun( ) ..\AbstractPart.php:175 9 0.0415 1634920 addImage ( ) ..\AbstractPart.php:222 10 0.0415 1635256 PhpOffice\PhpWord\Element\AbstractContainer->__call( ) ..\AbstractPart.php:222 11 0.0416 1641912 call_user_func_array:{C:\wamp\www\src\PhpWord\Element\AbstractContainer.php:101} ( ) ..\AbstractContainer.php:101 12 0.0417 1642312 PhpOffice\PhpWord\Element\AbstractContainer->addElement( ) ..\AbstractContainer.php:101 13 0.0425 1746592 newInstanceArgs ( ) ..\AbstractContainer.php:138 14 0.0425 1747896 PhpOffice\PhpWord\Element\Image->__construct( ) ..\AbstractContainer.php:138 15 0.0430 1809040 PhpOffice\PhpWord\Element\Image->checkImage( ) ..\Image.php:129 2) I have a problem. I can't read headers and footers. Help me please

ivanlanin commented 10 years ago
  1. Can you view the images in upload/generate_file.docx using a word processor?
  2. Headers and footers can be read from DOCX (see Sample_11_ReadWord2007.php), but they're not written to HTML.
hp3k commented 10 years ago

Okey, but how to get footer and header tekst and settings and assign to array or parse to html?

ivanlanin commented 10 years ago

Good question. From the object model, you can use getHeaders and getFooters method of a section, but it will be cumbersome. So the final result that you want is to have the content of footers and headers as HTML?

hp3k commented 10 years ago

Yes I don't know how I can get header and footer also how I can get values?

rajeshbabu54 commented 9 years ago

Hello everyone, I want to add some images and text to existing docx in phpword.I tried with the following code but it is not showing correct.

include_once 'Sample_Header.php'; // New Word document $name = basename(FILE, '.php'); $source = DIR . "/resources/{$name}.docx"; //$phpWord = new \PhpOffice\PhpWord\PhpWord(); $phpWord1 = \PhpOffice\PhpWord\IOFactory::load($source); $document = $phpWord1->loadTemplate($source); $document->saveAs('temp.docx'); $phpWord = \PhpOffice\PhpWord\IOFactory::load('temp.docx'); $section = $phpWord->addSection(); $section->addText(htmlspecialchars('Local image without any styles:')); $section->addImage('resources/_mars.jpg'); $section->addTextBreak(2); $source = 'http://php.net/images/logos/php-med-trans-light.gif'; $section->addText(htmlspecialchars("Remote image from: {$source}")); $section->addImage($source); $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'Word2007'); $xmlWriter->save('result1234566.docx');

Please send me the code. Please help me out from this problem

Thank you

Rajesh babu

ascendedcrow commented 7 years ago

I Get this error when replacing the attached docx with the code, even when fixing it, the images gets lost in saving the file: `<?php include_once 'Sample_Header.php';

// Read contents $name = basename(FILE, '.php'); $source = "D:\Temp\Letterhead.docx";

echo date('H:i:s'), " Reading contents from {$source}", EOL; $phpWord = \PhpOffice\PhpWord\IOFactory::load($source);

// Save file echo write($phpWord, basename(FILE, '.php'), $writers); if (!CLI) { include_once 'Sample_Footer.php'; }`

Warning: Division by zero in D:\Work\Test\PHPWebProject1\PHPWebProject1\vendor\phpoffice\phpword\src\PhpWord\Writer\ODText\Style\Paragraph.php on line 41

Letterhead.docx

94sukhbrar commented 3 years ago

Hi Please tell me how I read the content of the .doc or .docxfile.

gaby64 commented 2 years ago

Good question. From the object model, you can use getHeaders and getFooters method of a section, but it will be cumbersome. So the final result that you want is to have the content of footers and headers as HTML?

Could we have a HTML writer that outputs as pages, with headers and footers?

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue for you, please try to help by debugging it further and sharing your results. Thank you for your contributions.