cara export file html ke word menggunakan php

Cara Export File HTML ke Word Menggunakan PHP

Umumnya, fitur ekspor digunakan untuk mengunduh file HTML sebagai file dokumen MS Word dan menyimpannya untuk penggunaan offline. Konversi file HTML menjadi word document menggunakan kode PHP adalah fitur yang paling banyak diminta oleh developer. Format Microsoft Word atau Doc (.doc) ideal untuk mengekspor konten HTML dalam sebuah file. Fungsi ekspor ke doc dapat dengan mudah diimplementasikan menggunakan kode php.

Fungsi ekspor sisi server ke dokumen membuat aplikasi web mudah digunakan. Pengguna dapat mengekspor semua data konten halaman web ke dalam format doc. Dalam tutorial ini, kami akan menunjukkan Cara mengekspor file HTML ke dokumen MS Word (.doc) menggunakan kode php.

Ada instruksi pengkodean langkah demi langkah yang mudah tersedia untuk pengembang, yang ingin mengonversi file HTML ke dokumen MS word (.doc) menggunakan kode php. Itu semua bisa dilakukan oleh header PHP. Code dimulai dengan menyertakan application vnd.ms-word

header("Content-type: application/vnd.ms-word");
 
# And then replacing wordfile.doc with what you need the filename to default to.
 
header("Content-Disposition: attachment;Filename=Wordfile.doc");
 
header("Pragma: no-cache");
 
header("Expires: 0");

Ekspor HTML ke Dokumen MS Word

Langkah-1: Buat file index.php yang didalamnya terdapat struktur html dan sebuah form, seperti code berikut ini.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
 
<head>
 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 
<title>How to export HTML file to MS Word document (.doc) using php code</title>
 
<style>
 
h1 {
 
height: 50px;
 
width: 100%;
 
font-size: 18px;
 
background: #18aa8d;
 
color: white;
 
line-height: 150%;
 
border-radius: 3px 3px 0 0;
 
box-shadow: 0 2px 5px 1px rgba(0, 0, 0, 0.2);
 
}
 
form {
 
box-sizing: border-box;
 
width: 500px;
 
margin: 100px auto 0;
 
box-shadow: 2px 2px 5px 1px rgba(0, 0, 0, 0.2);
 
padding-bottom: 40px;
 
border-radius: 3px;
 
}
 
form h1 {
 
box-sizing: border-box;
 
padding: 20px;
 
}
 
.text-field {
 
margin: 40px 25px;
 
width: 80%;
 
display: block;
 
border: none;
 
padding: 10px 0;
 
-webkit-transition: all 0.3s cubic-bezier(0.64, 0.09, 0.08, 1);
 
transition: all 0.3s cubic-bezier(0.64, 0.09, 0.08, 1);
 
background: -webkit-linear-gradient(top, rgba(255, 255, 255, 0) 96%, #1abc9c 4%);
 
background: linear-gradient(to bottom, rgba(255, 255, 255, 0) 96%, #1abc9c 4%);
 
background-position: -100% 0;
 
background-size: 100% 100%;
 
background-repeat: no-repeat;
 
color: #0e6252;
 
}
 
.text-field:focus, .text-field:valid {
 
box-shadow: none;
 
outline: none;
 
background-position: 0 0;
 
border-bottom: solid 1px #1abc9c;
 
}
 
button {
 
float:right;
 
border: none;
 
background: #1abc9c;
 
cursor: pointer;
 
border-radius: 3px;
 
padding: 6px;
 
width: 40px;
 
color: white;
 
margin-right: 40px;
 
box-shadow: 0 3px 6px 0 rgba(0, 0, 0, 0.2);
 
}
 
button:hover {
 
-webkit-transform: translateY(-3px);
 
-ms-transform: translateY(-3px);
 
transform: translateY(-3px);
 
box-shadow: 0 6px 6px 0 rgba(0, 0, 0, 0.2);
 
} 
.text-field-1 {
 
background:none;
 
}
 
</style>
 
</head>
 
<body>
 
<form method="POST" action="<strong>export-to-word.php</strong>">
 
<h1>Form</h1>
 
<input placeholder="Heading" name="heading" class="text-field" type="text" required="">
 
<textarea rows="3" cols="6"  name="content" placeholder="content"  class="text-field text-field-1"></textarea>
 
<button type="submit" name="exportToWord">Export Doc</button> &nbsp;&nbsp;&nbsp;
 
</form>
 
</body>
 
</html>

Langkah-2: Selanjutnya kita buat file export-to-word.php untuk menerima data yang dikirim dari form index.php

<?php
 
if(isset($_POST['exportToWord'])){
 
header("Content-type: application/vnd.ms-word"); ?>
 
# replace Wordfile.doc with whatever you want the filename to default to
 
header("Content-Disposition: attachment;Filename=exporttoword.doc");
 
header("Pragma: no-cache");
 
header("Expires: 0");
 
$current_date = date('d-m-Y');
$heading = $_POST['heading'];
 
$content = $_POST['content'];
 
echo "<div style='font-size: 1em; line-height: 1.6em; color: #4E6CA3; padding:10px;' align='right'>Report Date: $current_date</div>";
 
echo "<div style='font-size: 1em; line-height: 1.6em; color: #4E6CA3; padding:10px;' align='left'>$heading</div>";
 
echo "<div style='font-size: 1em; line-height: 1.6em; color: #4E6CA3; padding:10px;' align='left'>$content</div>";;
 
}
 
?>

Selamat, sekarang Anda sudah bisa langsung export file html ke word dengan menggunakan bahasa pemrograman PHP. Jika ada yang ditanyakan, silahkan tulis di kolom komentar di bawah ini.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top