2017-07-21 6 views
0

私は、その場でTCPDFを使用してPDFを生成し、それをブラウザに表示しようとしています。私は既に持っている既にロードされているページにTCPDF pdfを生成して表示するには?

...

  • 出力任意のHTMLせずにダウンロード
  • 出力PDFインラインとしてPDF。 (HTML。

ことを一掃することなく、$pdf->Output('example_007.pdf', 'I');

私がやりたいことはある...

  • 出力、既に印刷されたHTMLとPDFをインラインで使用するには、当然のことながら、私は走りましたob_start、またはob_flushなどのob関数を使用してこのエラーを取り除くことができましたが、これによって既存のHTMLが消去されます。

    私は、AJAX(純粋なJavaScript)によって呼び出されたPHPファイルでPDFを出力しようとしました。しかし、私はその結末に運がなかった。何も現れなかった。

    コンテンツが既に出力されている間にブラウザにPDFを表示するにはどうすればよいですか?私は多かれ少なかれ、以下のようなものを探しています

    enter image description here

    を私は現在、ここで、テストページでこれをやっているが、私のコードです:...それの大半は、TCPDFのからであります例。あなたはそれはあなたがiFrameを使用してPDFファイルをロードしようとすることができインライン表示するために使用したのと同じ手法使用

    //The following PHP block was tried but also failed as it wiped existing HTML 
    <?php 
        ob_start(); 
    ?> 
    
    <!DOCTYPE html> 
    <html> 
        <head> 
         <title>Test PDF</title> 
        </head> 
        <body> 
         <h1>Before PDF</h1> 
         <?php 
          error_Reporting(E_ALL); 
    
          date_default_timezone_set("America/New_York"); 
    
          // Include the main TCPDF library (search for installation path). 
          require_once $_SERVER["DOCUMENT_ROOT"]."/my-site/TCPDF-master/tcpdf.php"; 
    
          //ob_start() Tried this at some point... Didn't work 
    
          // create new PDF document 
          $pdf = new TCPDF(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 001'); 
          $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.' 001', PDF_HEADER_STRING, 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 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 default font subsetting mode 
          $pdf->setFontSubsetting(true); 
    
          // 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', '', 14, '', true); 
    
          // Add a page 
          // This method has several options, check the source code documentation for more information. 
          $pdf->AddPage(); 
    
          // set text shadow effect 
          $pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal')); 
    
          // Set some content to print 
          $html = " 
           <h1>Hello World!</h1> 
          "; 
    
          // Print text using writeHTMLCell() 
          $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); 
    
          // --------------------------------------------------------- 
    
          // Close and output PDF document 
          // This method has several options, check the source code documentation for more information. 
    
          //ob_clean(); Various posts suggested various combinations of these methods 
          //ob_flush(); 
          $pdf->Output('example_007.pdf', 'I'); 
          //ob_end_flush(); 
          //ob_end_clean(); 
         ?> 
        </body> 
    </html> 
    

答えて

1

<html> 
    <head> 
    </head> 
    <body> 
     <p>some content</p> 
     <iframe src="/path/to/your/pdf/script.php"></iframe> 
    </body> 
</html> 
+0

を私はできますが、私は、HTMLのobject要素を使用していじりをたくさんやりましたIframeを使用してみてください。それはそれで回り込むのに時間が必要です。 – Tyler

+0

@gp_sfloverこれは、明確化の要求よりも、質問の形の答えのほうが多かった。しかし、明確化 –

+0

+1と想像上のケーキのおかげで。オブジェクト要素を使ってみました。しかし、PDFコードを別のファイルの中に入れてから、そのファイルにsrc/data属性を渡すことは決してありません。その方法は機能しているようです。 – Tyler

関連する問題