2009-07-30 9 views
13

PHP로 작성된 웹 페이지에서 pdf 파일을 만들고 싶습니다. 내 문서는 mysql에서 생성하고 PDF 파일을 생성해야합니다.이 PDF를 저장하고 읽고 싶습니다. 코드 샘플을 제공하십시오.PHP로 PDF 파일 만들기

+1

[TCPDF] (http://www.tecnick.com/public/code/cp_dpage.php?aocp_dp=tcpdf) 라이브러리를 사용하여 PDF를 생성 할 수 있습니다. 이 사이트에는 또한 몇 가지 코드 예제가 있습니다. db에서 데이터를 가져 와서 조작하는 경우에는 직접 코딩해야합니다. – RaYell

+0

[fpdf] (http://www.fpdf.org) 사이트에서 제공되는 샘플 코드를 사용하여 PDF를 만드는 가장 간단한 방법을 제공합니다. 여기 예제 [http://www.fpdf.org/en/script/script10.php](http://www.fpdf.org/en/script/script10.php) – Milap

+0

@bb에서 찾을 수 있습니다. 내 질문 좀 봐 줄래 http://stackoverflow.com/questions/16925076/php-creating-a-pdf-how-can-i-get-this-working-errors-populating/16925161?noredirect=1# 16925161 – ValleyDigital

답변

13

TCPDF 라이브러리 사용 :

<? 
require_once("tcpdf/tcpdf.php"); 
$pdf = new TCPDF(); 

// set document information 
$pdf->SetCreator(PDF_CREATOR); 
$pdf->SetAuthor("my name"); 
$pdf->SetTitle("my doc"); 
$pdf->SetSubject("x"); 
$pdf->SetKeywords("a, b, c"); 


// set default header data 
$pic = "/gfx/header.png"; 
$pdf->SetHeaderData(realpath($pic), "25", "Title"); 

// set header and footer fonts 
$pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); 
$pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); 

//set margins 
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); 
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER); 
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER); 

    //set auto page breaks 
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); 

//set image scale factor 
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 

//set some language-dependent strings 
$pdf->setLanguageArray($l); 

//initialize document 
$pdf->AliasNbPages(); 

// add a page 
$pdf->AddPage(); 

// --------------------------------------------------------- 

// set font 
$pdf->SetFont("helvetica", "", 12); 

// set a background color 
$pdf->SetFillColor(230, 240, 255, true); 


$pdf->SetFont("", "b", 16); 
$pdf->Write(16, "some text\n", "", 0, 'C'); 

$pdf->Output("filename.pdf", "I"); 
?> 
+0

나는이 코드를 테스트한다. 그러나 $ pdf-> Write (16, "some text"\ n ","0, 'C'); pdf ","I "); 이 줄. 어떻게 해결합니까? – thinzar

+0

"너무 많습니다. 죄송합니다. 지금 수정되었습니다 –

1

나는 이미 HTML 페이지가를, 당신이 HTML2PDF 같은 도구를 사용하는 수도를위한 가장 빠른 방법 대신 생성하는 PDF로 그 HTML 데이터를 "변환"하는 "처음부터"PDF 파일.

해당 페이지를 인용 :

그것은 PDF 형식의 유효한 HTML 4.01의 전환을 허용하고, LGPL 하에서 배포됩니다.

이 라이브러리는 주로 처리되었으며 송장 생성과 다른 공식 문서가 얽혀 있습니다. 아직 모든 태그를 가지고 있지 않다. .

HTML의 몇 가지 예와 해당 PDF 파일이 있으므로 가능한 기능을 볼 수 있습니다.

+0

어떻게 html2pdf에 페이지 번호를 추가 할 수 있습니까? – kedomonzter

0

https://github.com/tecnickcom/tc-lib-pdf에서 TCPDF 소스 코드의 최신 버전을 다운로드하십시오. 소스 코드 자체에는 예제라는 폴더가있는 디렉토리가 있습니다. 당신은 예제에서 코드를 체크 아웃 할 수 있습니다. 그렇지 않으면 아래 코드를 시도 할 수 있습니다.

<?php 

require_once('tcpdf/config/tcpdf_config.php'); 
require_once('tcpdf/tcpdf.php'); 

// create new PDF document 
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 

// set default font subsetting mode 
$pdf->setFontSubsetting(true); 
// set margins 
//$pdf->SetHeaderMargin(PDF_MARGIN_HEADER); 
//$pdf->SetFooterMargin(PDF_MARGIN_FOOTER); 
// set default header data 
$pdf->SetHeaderData('', '', 'Hello ','Gudmrng', array(0,64,255), array(0,64,128)); 
$pdf->setFooterData(array(0,64,0), array(0,64,128)); 

// set header and footer fonts 
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); 
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); 

// set default monospaced font 
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); 

// set margins 
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); 
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER); 
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER); 

// set auto page breaks 
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); 

// Set font 
// dejavusans is a UTF-8 Unicode font, if you only need to 
// print standard ASCII chars, you can use core fonts like 
// helvetica or times to reduce file size. 
$pdf->SetFont('dejavusans', '', 10, '', true); 
//  $pdf->SetFont('helvetica', '', 8); 
    // Add a page 
// This method has several options, check the source code documentation for more information. 
$pdf->AddPage(); 
$tbl_header = '<h2 style="color:blue;text-align: center;font-size: 14px;">Headding</h2></br>'; 

$html = '<table width="530" cellspacing="0" cellpadding="3" border="1"> 
        <tr> 
        <th style="text-align: center;font-size: 10px;font-weight: bold;width:100px;">heading 1</th> 
        <th style="text-align: center;font-size: 10px;font-weight: bold;width:122px;">heading 2</th> 
        </tr>'; 
         $html .= '<tr>'; 
         $html .= '<td><b>Hello World</b></td>'; 
         $html .= '<td><b>Helo </b></td>'; 
         $html .= '</tr>'; 
         $html .= '</table>'; 


$pdf->writeHTML($tbl_header . $html, true, false, false, false, ''); 
//$pdf->writeHTML($html, true, 0); 
//$pdf->writeHTML($html, true, 0); 


    $pdf->Output('test_1.pdf','I'); 

?> 
0

나는이를 위해 CutyCapt를 사용했습니다.

  1. a CutyCapt을 다운로드하여 설치하십시오! 및 xvfb-run.
  2. 메이크업 PDF를 HTML 페이지에 스타일과 다음이 명령으로이 페이지에서 PDF를 만들 :

Xvfb가 운영하는 cutycapt --min 폭 = 1485 --url = 'html로 URL'--out = ' pdf 경로