PHP Word Template Save As PDF ( DomPDF )

<?php

require 'vendor/autoload.php';

$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('w.docx');


$values = [
    ['nim' => 001, 'nama' => 'a', 'alamat' => 'alamat a'],
    ['nim' => 002, 'nama' => 'b', 'alamat' => 'alamat b'],
    ['nim' => 003, 'nama' => 'c', 'alamat' => 'alamat c'],
    ['nim' => 004, 'nama' => 'd', 'alamat' => 'alamat d'],
];
$templateProcessor->cloneRowAndSetValues('nim', $values,);

$templateProcessor->setValues([
    'nimx' => 'Muhamamd Ullil fahri',
    'namax' => '31',
    'alamatx' => 'Ketapang , Kaliamnta Barat',

]);

//header("Content-Disposition: attachment; filename=template.docx");

//$templateProcessor->saveAs('php://output');
$pathToSave = 'ok.docx';
$templateProcessor->saveAs($pathToSave);


// Require composer autoloder.
require __DIR__ . '/vendor/autoload.php';

use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Settings;

// Make sure you have `dompdf/dompdf` in your composer dependencies.
Settings::setPdfRendererName(Settings::PDF_RENDERER_DOMPDF);
// Any writable directory here. It will be ignored.
Settings::setPdfRendererPath('.');

$phpWord = IOFactory::load('ok.docx', 'Word2007');
$phpWord->save('document.pdf', 'PDF');

echo "<script>window.location='document.pdf'</script>";;

 980 total views

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *