Hướng dẫn phpword

Gợi ý: Để kết quả Tra cứu chính xác hơn, bạn có thể tìm trong số hiệu hoặc tiêu đề của Văn bản và chọn tìm theo chích xác cụm từ. Nếu bạn gặp khó khăn trong việc Tra cứu, vui lòng bấm vào đây để được hỗ trợ trực tiếp từ Thư viện Pháp Luật hoặc bấm vào đây để yêu cầu Văn bản bạn cần.

Bạn muốn tải một trang html xuống với định dạng .doc, nếu bạn là một php developer chắc hẳn việc đầu tiên bạn nghĩ tới là thư viện PHPword, mình cũng đã nghĩ và thử với thư viện này, nhưng điều bất cập là bị lỗi font, thử nhiều cách và không thành công, cộng với việc tạo văn bản, nội dung khá rắc rối.

Bài viết này mình sẽ hướng dẫn các bạn sử dụng hàm header sẵn có của php để làm việc này.


- Ưu điểm:

+ Nhanh, gọn, dễ dàng.

+ Không phải nhúng bất kì thư viện nào khác.


- Nhược điểm:

+ Gặp vấn đề với căn lề, mặc định nó sẽ full width.


Demo ảnh:

Trang HTML
File Word

Hướng dẫn

Dán đoạn code trên vào đầu trang như ảnh dưới.

 header("Content-Type: application/vnd.ms-word");
 header("Expires: 0");
 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
 header("content-disposition: attachment;filename=TranDucIT.doc");
?>



Khi chạy với đường dẫn trang trên, trang đó sẽ được tự động tải xuống với định dạng .doc 

Bạn có thể tùy biến code để nó hoạt động theo ý.


Tác giả: TranDuc

Việc tích hợp soạn thảo file văn bản Word tích hợp vào CodeIgniter thật đơn giản; bạn có thể làm theo hướng dẫn như sau:

  • Bước 1: Download dữ liệu sau:
Download CodeIgniter Download PHPWord
  • Bước 2:  Giải nén  PHPWord (chỉ giải nén PHPWord.php and PHPWord folder) và đưa vào thư mục application/third_party của CodeIgniter

Hướng dẫn phpword

Chỉ copy 2 thư mục vào application/third_party của bạn; kết quả như sau:

Hướng dẫn phpword

Thư mục third_party sau khi thêm PHPWord

  • Bước 3: Bây giờ bạn tạo 1 thư viện mới trong application/libraries của CodeIgniter. ví dụ đặt tên là Word.php. Mã nguồn như sau:

1

2

3

4

5

6

7

8

9

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 

require_once APPPATH."/third_party/PHPWord.php";

 

class Word extends PHPWord {

    public function __construct() {

        parent::__construct();

    }

}

  • Vậy là xong.
  • Bước 4: Bây giờ chúng ta gọi và sử dụng thư viện Word từ Controller của bạn:
  • Gọi/Load thư viện và tạo ra section mới (Bắt buộc)

1

2

3

$this->load->library('word');

//our docx will have 'lanscape' paper orientation

$section = $this->word->createSection(array('orientation'=>'landscape'));

  • Thêm một số định dạng và đoạn văn bản cho nó!

1

2

3

4

5

6

7

8

9

10

11

// Add text elements

$section->addText('Hello World!');

$section->addTextBreak(1);

$section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));

$section->addTextBreak(1);

$this->word->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));

$this->word->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));

$section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle');

$section->addText('I have only a paragraph style definition.', null, 'pStyle');

  • Thêm một số ảnh! Chú ý không sử dụng base_url or site_url, PHPWord chỉ sử dụng đường dẫn tuyêt đối đến máy chủ của bạn (Có nghĩa rằng bạn không thể thêm ảnh mà không phải từ máy chủ của bạn).

1

2

3

4

5

6

7

8

// Add image elements

$section->addImage(FCPATH.'/image/_mars.jpg');

$section->addTextBreak(1);

$section->addImage(FCPATH.'/image/_earth.JPG', array('width'=>210, 'height'=>210, 'align'=>'center'));

$section->addTextBreak(1);

$section->addImage(FCPATH.'/image/_mars.jpg', array('width'=>100, 'height'=>100, 'align'=>'right'));

  • Tạo bảng table và đưa thêm 1 số định dạng style

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

// Define table style arrays

$styleTable = array('borderSize'=>6, 'borderColor'=>'006699', 'cellMargin'=>80);

$styleFirstRow = array('borderBottomSize'=>18, 'borderBottomColor'=>'0000FF', 'bgColor'=>'66BBFF');

// Define cell style arrays

$styleCell = array('valign'=>'center');

$styleCellBTLR = array('valign'=>'center', 'textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR);

// Define font style for first row

$fontStyle = array('bold'=>true, 'align'=>'center');

// Add table style

$this->word->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);

// Add table

$table = $section->addTable('myOwnTableStyle');

// Add row

$table->addRow(900);

// Add cells

$table->addCell(2000, $styleCell)->addText('Row 1', $fontStyle);

$table->addCell(2000, $styleCell)->addText('Row 2', $fontStyle);

$table->addCell(2000, $styleCell)->addText('Row 3', $fontStyle);

$table->addCell(2000, $styleCell)->addText('Row 4', $fontStyle);

$table->addCell(500, $styleCellBTLR)->addText('Row 5', $fontStyle);

// Add more rows / cells

for($i = 1; $i <= 2; $i++) {

$table->addRow();

$table->addCell(2000)->addText("Cell $i");

$table->addCell(2000)->addText("Cell $i");

$table->addCell(2000)->addText("Cell $i");

$table->addCell(2000)->addText("Cell $i");

$text = ($i % 2 == 0) ? 'X' : '';

$table->addCell(500)->addText($text);

}

  • Cuối cùng, Đặt tên văn bản với đuôi  .docx để người sử dụng lưu trên máy tính cá nhân.

1

2

3

4

5

6

7

$filename='just_some_random_name.docx'; //save our document as this file name

header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); //mime type

header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name

header('Cache-Control: max-age=0'); //no cache

 

$objWriter = PHPWord_IOFactory::createWriter($this->word, 'Word2007');

$objWriter->save('php://output');

  • Kết quả của ví dụ này như sau (mở với  Ms Word 2007 và xem ở tỷ lệ 50%)

Hướng dẫn phpword

Chúc các bạn thành công!

Link tham khảo: http://www.ahowto.net/php/creating-ms-word-document-using-codeigniter-and-phpword/

Mọi thông tin vui lòng gửi email: dinhanhtuan68@gmail,com