PHPOffice / PHPWord

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

Inquiry on PHPWord Features for TOC, Tiny Editor, Margin, image ,and Page Numbers in DOCX Generation #2521

Open Khushboo75 opened 11 months ago

Khushboo75 commented 11 months ago
<?php
require "vendor/autoload.php";

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Element\TOC;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\Style\ListItem;

$phpWord = new PhpWord();
$phpWord->setDefaultFontName('Calibri');
$phpWord->setDefaultFontSize(12);

$marginSettings = [
    'marginLeft' => 720,
    'marginRight' => 720,
    'marginBottom' => 720,
    'marginTop' => 720,
];

$apiUrl = 'https://jsonplaceholder.typicode.com/photos';
$response = file_get_contents($apiUrl);

$data = json_decode($response, true);

$section = $phpWord->addSection($marginSettings);
/*
* ISSUE- 1: Dynamic page numbers are not rendering appropriately within the Table of Contents (TOC) section (issue is uncommented)
*/

$section->addTOC(array('tabPos' => 720, 'hanging' => 360));

$section->addTitle('Test 1', 1, 5);
for ($i = 0; $i < 10; $i++) {
    $item = $data[$i];

    $html = '<p><strong>Album ID:</strong> ' . $item['albumId'] . '</p>' .
        '<p><strong>ID:</strong> ' . $item['id'] . '</p>' .
        '<p><strong>Title:</strong> ' . $item['title'] . '</p>' .
        '<p><strong>URL:</strong> ' . $item['url'] . '</p>';
    /*
        * ISSUE-2: image is not working (issue is commented)
        */

    // '<p><strong>Thumbnail URL:</strong> <img src="' . $item['thumbnailUrl'] . '" alt="Thumbnail"></p>';
    /*
        * ISSUE-3 : We would also use the image but it was not working (issue is commented)
        */

    // <img src="data:' . $self_service_img["img_type"] . ';base64,' .  $item['thumbnailUrl']  . '" width="657" height="830" alt="Self Service Digitalization with CLaaS@Work Image"/>

    \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);
}
$section->addPageBreak();

// Add a title
$section->addTitle('Test 2', 1, 5);
/*
        * ISSUE- 4 : Tiny Editor code is not working after creating the file (issue is commented)
        */

// $html =
//     '<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
// Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer 
// took a galley of type and scrambled it to make a type specimen book. It has surviv Ipsum \"Digital Marketing\" Lorem 
// Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy 
// text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen, but also 
// the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of
//  Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker 
//  including versions of Lorepsum.</p>\n<p>The course \"Digital Transformation\" covers a comprehensive set of instructional 
//  units that empower learners to navigate the digital realm successfully. In the unit \"Innovative Venture Creation,\" Lorem 
//  Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text
//   ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has 
//   survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in
//    the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like 
//    Aldus PageMaker including versions of Lorem Ipsum.</p>\n<p>The unit on \"Business Model Innovation\" explores how businesses can adapt their
//     existing models to address digital disruptions and changing consumer behaviors. Learners analyze and redesign business models, leveraging digital 
//     technologies and emerging trends to ensure sustainability and competitiveness in the market.

/*
        * ISSUE- 5 :"&" letter   is not working after creating the file (issue is commented)
        */
// $html ='<p>>Lorem Ips & </p>';
// Add the HTML content to the section
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);

$file = 'test.docx';
$objWriter = IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save($file);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Content-Length: ' . filesize($file));

readfile($file);