PHPWord Library PHP pengolah file docx

Library PHPWord dapat ditemukan pada paket PHPOffice, paket ini berisi library PHPWord, PHPSpreadsheet, PHPpresentations, dan lainya dapat dilihat pada situs pengembangnya https://github.com/PHPOffice.

Library ini dapat mengelola dokumen word dan yang kompitable dengannya seperti OOXML, OpenXML, ODF, RTF, HTML, ataupun PDF.

Untuk instalasinya dapat menggunakan composer, [cara install composer].

Untuk istalasinya dapat mengikuti langkah berikut.

Buka comand promp

pada jendela commad promp buka direktori yang akan diinstalkan library PHPWord, sebagai contoh, saya memiliki lokal website yang berada pada direktory “C:\laragon\www\BelajarPHPWord”, pada jendela command promp ketik baris perintah dibawah dan tekan enter.

cd C:\laragon\www\BelajarPHPWord

Bila sudah berada pada direktory kerja lanjut ke baris perintah penginstalan PHPWord dengan mengetik baris perintah berikut pada command promp dan tunggu hingga proses instalasinya.

composer require phpoffice/phpword

dan hasilnya akan terbentuk direktori vendor pada direktori website kalian

Pada tahap ini penginstalan PHPWord telah berhasil. Sebagai contoh kita dapat menggunakan file example yang di sediakan https://github.com/PHPOffice/PHPWord#getting-started atau salin code berikut dan beri nama index.php, perbedaan pada contoh dengan code dibawah terletak pada baris ke 2 pada contoh menggunakan

require_once 'bootstrap.php';
<?php
require_once 'vendor/autoload.php';

// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();

/* Note: any element you append to a document must reside inside of a Section. */

// Adding an empty Section to the document...
$section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
$section->addText(
    '"Learn from yesterday, live for today, hope for tomorrow. '
        . 'The important thing is not to stop questioning." '
        . '(Albert Einstein)'
);

/*
 * Note: it's possible to customize font style of the Text element you add in three ways:
 * - inline;
 * - using named font style (new font style object will be implicitly created);
 * - using explicitly created font style object.
 */

// Adding Text element with font customized inline...
$section->addText(
    '"Great achievement is usually born of great sacrifice, '
        . 'and is never the result of selfishness." '
        . '(Napoleon Hill)',
    array('name' => 'Tahoma', 'size' => 10)
);

// Adding Text element with font customized using named font style...
$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle(
    $fontStyleName,
    array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
);
$section->addText(
    '"The greatest accomplishment is not in never falling, '
        . 'but in rising again after you fall." '
        . '(Vince Lombardi)',
    $fontStyleName
);

// Adding Text element with font customized using explicitly created font style object...
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
$myTextElement->setFontStyle($fontStyle);

// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

// Saving the document as ODF file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('helloWorld.odt');

// Saving the document as HTML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('helloWorld.html');

/* Note: we skip RTF, because it's not XML-based and requires a different example. */
/* Note: we skip PDF, because "HTML-to-PDF" approach is used to create PDF documents. */

code diatas akan membentuk 3 jenis file yaitu “helloWorld.docx, helloWorld.odt ,dan helloWorld.html” dan sekarang struktur direktori kalian telah bertambah 1 file dengan nama index.php,

bila file index.php di jalankan maka 3 file yang telah dijelaskan sebelumnya akan terbentuk, walaupun pada jendela tidak akan menampilkan apapun 3 file tersebut akan terbentuk.

Tinggalkan komentar