2016-07-22 25 views
1

我正在嘗試創建一個文檔,將在線查看PDF格式。示例URL將爲url.com/PDF.php?name=John Doe正確的方式回聲到預定義的pdf

PDF將包含大量預定義的文本,並且我想多次將name=後面的部分回顯到PDF中。

我讀過(並試過)FPDF,TCPDF,但我不知道如何實現這一點。

<?php 
    echo htmlentities($_GET["name"]); //John 
?> 

有沒有人知道一個簡單的方法來實現這一目標?

我沒有PDF代碼,因爲我沒有得到FPDF或TCPDF來正確工作/顯示我在做什麼。以爲我會檢查這是否是正確的方式去,或者如果我應該使用純粹的PHP。

A'for dummies'guide to make these php/pdf libraries would actually be great。我下載庫,上傳到我的服務器,從示例文件夾複製其中一個文件,將路由更改爲(tcpdf-relative)文件,但不行,它不起作用。如果我轉到example/example_001.pdf中的其中一個文件,它會正確顯示。如果我將該文件複製到根目錄並將鏈接更改爲匹配新位置,則它不起作用。

例的偉大工程 http://fragger.no/pdf/examples/example_003.php

這是擺在根本不顯示可言,只有一個白色的頁面文件。沒有錯誤。

<?php 

// Include the main TCPDF library (search for installation path). 
require_once('examples/tcpdf_include.php'); 


// Extend the TCPDF class to create custom Header and Footer 
class MYPDF extends TCPDF { 

    //Page header 
    public function Header() { 
     // Logo 
     $image_file = K_PATH_IMAGES.'logo_example.jpg'; 
     $this->Image($image_file, 10, 10, 15, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false); 
     // Set font 
     $this->SetFont('helvetica', 'B', 20); 
     // Title 
     $this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M'); 
    } 

    // Page footer 
    public function Footer() { 
     // Position at 15 mm from bottom 
     $this->SetY(-15); 
     // Set font 
     $this->SetFont('helvetica', 'I', 8); 
     // Page number 
     $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M'); 
    } 
} 

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

// set document information 
$pdf->SetCreator(PDF_CREATOR); 
$pdf->SetAuthor('Nicola Asuni'); 
$pdf->SetTitle('TCPDF Example 003'); 
$pdf->SetSubject('TCPDF Tutorial'); 
$pdf->SetKeywords('TCPDF, PDF, example, test, guide'); 

// set default header data 
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING); 

// 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 image scale factor 
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 

// set some language-dependent strings (optional) 
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) { 
    require_once(dirname(__FILE__).'/lang/eng.php'); 
    $pdf->setLanguageArray($l); 
} 

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

// set font 
$pdf->SetFont('times', 'BI', 12); 

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

// set some text to print 
$txt = <<<EOD 
TCPDF Example 003 

Custom page header and footer are defined by extending the TCPDF class and overriding the Header() and Footer() methods. 
EOD; 

// print a block of text using Write() 
$pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0); 

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

//Close and output PDF document 
$pdf->Output('pdf.pdf', 'I'); 

//============================================================+ 
// END OF FILE 
//============================================================+ 

我編輯此: //包含主TCPDF庫(搜索安裝路徑)。 require_once('examples/tcpdf_include.php'); require_once

到 //包含主要的TCPDF庫(搜索安裝路徑)。 require_once('tcpdf.php');

它的工作,所以現在我需要的是在文檔中使用GET信息。

這是我喜歡的顯示名稱=

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

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

// set some text to print 
$txt = <<<EOD 
My text 

**NAME VALUE** 

Some more text 
EOD; 
+0

有一個答案爲您的問題,如果它我有用的不要忘記點擊它的複選標記來接受它。 –

回答

0

首先,爲了使用$_GET你必須檢查它是否存在?

<?php 

if (isset($_GET[ "name" ])) 
    $name = $_GET[ "name" ]; // ◄■■■■■■ NAME FROM URL. 
else $name = "noname"; // ◄■■■■■■ "NONAME" OR "?" OR ANYTHING ELSE. 

// Include the main TCPDF library (search for installation path). 
require_once('examples/tcpdf_include.php'); 
. 
. 
. 

名稱是存儲在$name,所以你可以在任何地方使用該變量:

$txt = <<<EOD 
My text 

$name 

Some more text 
EOD; 
+1

謝謝你親切的先生!這正是我想要達到的!我真誠地感謝你! –