1

I work on docx uploaded by users and I want to add line numbering (like this: http://prntscr.com/n5rc1s) on the docx before exporting it in PDF.

I tried to add this with PhpOffice but I did not succeed.

My code can convert in PDF but without line numbering.

Thanks for your help :)

My code:

require_once('vendor/autoload.php');


define('PHPWORD_BASE_DIR', realpath(__DIR__));
$domPdfPath = realpath(PHPWORD_BASE_DIR . '/vendor/dompdf/dompdf');
\PhpOffice\PhpWord\Settings::setPdfRendererPath($domPdfPath);
\PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF');

$phpWord = new \PhpOffice\PhpWord\PhpWord();

//Open template and save it as docx
$document = $phpWord->loadTemplate('test.docx');
$document->saveAs('temp.docx');

//Load temp file
$phpWord = \PhpOffice\PhpWord\IOFactory::load('temp.docx');
$sections = $phpWord->getSections();
foreach ($sections as $section) {
    $section->getStyle()->setLineNumbering(array());

}
//Save it
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
$xmlWriter->save('result.pdf');
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80

1 Answers1

0

You're close, but you have to pass values to

$section->getStyle()->setLineNumbering(array());

instead of an empty array. In the source

/**
 * Line numbering
 *
 * @var \PhpOffice\PhpWord\Style\LineNumbering
 * @see  http://www.schemacentral.com/sc/ooxml/e-w_lnNumType-1.html
 */
private $lineNumbering;

there's a reference to the schema, so you actually have to pass values for countBy, etc. You probably want at least some spacing between the text and the line numbers, so you can do something like

$section->getStyle()->setLineNumbering(['countBy' => 1, 'restart' => 'continuous', 'distance' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(.2)]);
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80