Skip to content

Issue with Footnotes Display in PhpWord #2631

Closed
@FaheemKGNINFOTECH

Description

@FaheemKGNINFOTECH

Describe the bug and add attachments

When attempting to add multiple footnotes in close proximity within a PhpWord document, only the second footnote appears at the bottom of the page. The first footnote does not display properly.

Expected behavior

Both footnotes should appear at the bottom of the page, displaying the respective footnote texts associated with the text runs.

Steps to reproduce

Create a PhpWord document.
Add two or more footnotes using addFootnote() method in close succession.
Save and generate the document.
Open the generated document and observe the footnotes at the bottom of the page.

Code
public function downloadDocument()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();

$footercellStyle = ['name' => 'Calibri', 'size' => 10];
$paragraphStyle = [
    'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::LEFT,
    'spaceAfter' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(1),
    'spaceBefore' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(1)
];

// Add first text run with footnote
$textrunFootnote1 = $section->addTextRun($paragraphStyle);
$textrunFootnote1->addText('Here is some text with a footnote.');
$footnote1 = $textrunFootnote1->addFootnote();
$footnote1->addText("Footnote 1: This is the first footnote.", $footercellStyle);

// Add some text to ensure separation
$section->addText('Some additional text to ensure separation between footnotes.', null, $paragraphStyle);
$section->addTextBreak();

// Add second text run with footnote
$textrunFootnote2 = $section->addTextRun($paragraphStyle);
$textrunFootnote2->addText('Here is some text with another footnote.');
$footnote2 = $textrunFootnote2->addFootnote();
$footnote2->addText("Footnote 2: This is the second footnote.", $footercellStyle);

// Set headers for file download
header("Content-Description: File Transfer");
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="footnote.docx"');
header('Cache-Control: max-age=0');
header('Pragma: public');

// Create a temporary file in the system's temporary directory
$tempFile = tempnam(sys_get_temp_dir(), 'phpword');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save($tempFile);

// Download the file
readfile($tempFile);
unlink($tempFile); // Delete the temp file after downloading
exit;

}

PHPWord version(s) where the bug happened

"phpoffice/phpword": "^1.2",

PHP version(s) where the bug happened

"php": "^8.1",

Priority

  • I want to crowdfund the bug fix (with @algora-io) and fund a community developer.
  • I want to pay the bug fix and fund a maintainer for that. (Contact @Progi1984)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions