2012-02-12 59 views
8

我想要做的是準備PHP生成的結果打印與強大的規則。在PHP中使用PDF

嘗試所有可能的方式與CSS + HTML:設置尺寸在PX,毫米,釐米。 沒有幫助。每個瀏覽器,甚至每臺打印機都打印出完全不同的紙張結果(使用&(不帶邊框打印)也嘗試過,也沒有得到結果)。經過長時間的研究,發現CSS不是實現此目的和更好的方式的最佳途徑 - 使用PHP創建PDF創建功能。所以,安裝TCPDF。但是無法使用我爲HTML輸出創建的邏輯部分來工作。

我想從上邊距和紙底雙方必須11毫米
  • 之間行保證金獲取
    • 表的第一行和最後一行0毫米
    • 表的什麼行必須在4毫米左右紙邊
    • 2毫米每列
    • 38毫米寬×21.2毫米高度每個小區
    • 13 x行,5×列之間服務,13x5 = 65個細胞
    • 在新的頁面中的每個表。換句話說 - 每個表分頁符
    • 在每個小區中39碼條碼後(值必須是$id
    • 在PDF結果只有表 - 沒有頁眉,頁腳沒有,沒有標題...等

    這裏有更詳細的解釋上的圖像:

    enter image description here

    什麼我越來越

    表單提交後,在PHP端處理需要很長的時間 - 大約一分鐘,打開空白頁而不是PDF結果。

    代碼:

    (代碼也不是那麼巨大,評論使它看起來像這樣:)

    <?php 
    require_once('tcpdf/config/lang/eng.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); 
    
    $pdf->SetPrintHeader(false); 
    $pdf->SetPrintFooter(false); 
    // set document information 
    $pdf->SetCreator(PDF_CREATOR); 
    $pdf->SetAuthor('John Smith'); 
    $pdf->SetTitle(false); 
    $pdf->SetSubject(false); 
    $pdf->SetKeywords(false); 
    
    // set default header data.set all false because don't want to output header footer 
    $pdf->SetHeaderData(false, false, false, false); 
    
    // 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(4, 11, 4); 
    $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); 
    
    // --------------------------------------------------------- 
    // set font 
    $pdf->SetFont('helvetica', '', 10); 
    
    // add a page 
    $pdf->AddPage(); 
    
    // define barcode style 
    $style = array(
        'position' => '', 
        'align' => 'C', 
        'stretch' => false, 
        'fitwidth' => true, 
        'cellfitalign' => '', 
        'border' => true, 
        'hpadding' => 'auto', 
        'vpadding' => 'auto', 
        'fgcolor' => array(0, 0, 0), 
        'bgcolor' => false, //array(255,255,255), 
        'text' => true, 
        'font' => 'helvetica', 
        'fontsize' => 8, 
        'stretchtext' => 4 
    ); 
    
    ob_start(); 
    ?> 
    
    
    
    <style type="text/css"> 
    
        table { 
    
         width: 100%;   
    
         border-collapse: collapse; 
    
        } 
    
        td img { 
         height:10mm; 
        } 
    
        td { 
         padding: 0 1mm 0 1mm; 
         vertical-align:middle;    
        } 
    
        .cell { 
         width: 38mm; 
         height:21mm; 
         font-style: bold; 
         text-align: center; 
        } 
    
        tr { 
         height:21mm; 
         margin:0; 
         padding:0;    
        } 
    
    
    </style> 
    
    <?php 
    
    $i = 0; 
    $item = new item($db); 
    foreach ($_POST['checkbox'] as $id) { 
        $details = $item->getDetails($id); 
        $qt = (isset($_POST['qt'])) ? $_POST['qt'] : $details['qt']; 
        for ($cnt = 1; $cnt <= $qt; $cnt++) { 
         // check if it's the beginning of a new table 
         if ($i % 65 == 0) 
          echo '<table>'; 
    
         // check if it's the beginning of a new row 
         if ($i % 5 == 0) 
          echo '<tr>'; 
    
         echo '<td><div class="cell">www.fety.fr<br/>'; 
         $pdf->Cell(0, 0, 'CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9', 0, 1); 
         $pdf->write1DBarcode($id, 'C39', '', '', '', 18, 0.4, $style, 'N'); 
         $pdf->Ln(); 
         echo '<br/>' . $details['hcode'] . '</div></td>'; 
    
         // check if it's the end of a row 
         if (($i + 1) % 5 == 0) 
          echo '</tr>'; 
    
         // check if it's the end of a table 
         if (($i + 1) % 65 == 0) 
          echo '</tr></table>'; 
    
         $i++; 
        } 
    } 
    
    // if the last table isn't full, print the remaining cells 
    if ($i % 65 != 0) { 
        for ($j = $i % 65; $j < 65; $j++) { 
         if ($j % 65 == 0) 
          echo '<table>'; 
         if ($j % 5 == 0) 
          echo '<tr>'; 
         echo '<td></td>'; 
         if (($j + 1) % 5 == 0) 
          echo '</tr>'; 
         if (($j + 1) % 65 == 0) 
          echo '</table>'; 
        } 
    } 
    
    $markup = ob_get_clean(); 
    
    // output the HTML content 
    $pdf->writeHTML($markup, true, false, true, false, ''); 
    
    
    
    // reset pointer to the last page 
    $pdf->lastPage(); 
    
    // --------------------------------------------------------- 
    //Close and output PDF document 
    $pdf->Output('bcsheet.pdf', 'I'); 
    ?> 
    

    腳本就像是:

    1. 用戶選擇項複選框
    2. 表單提交後,PHP通過Ajax獲取複選框的值
    3. 在f oreach循環,PHP從數據庫中獲取每個項目的數量。
    4. 生成的表
    +0

    準備給100代表解決此問題 – heron 2012-02-12 11:59:24

    +0

    您是否考慮過使用原始PostScript?這是一項具有挑戰性的任務,但您可以完全控制渲染畫布。你也可以找到渲染條形碼的庫/教程。 – Tibo 2012-02-12 12:01:01

    +0

    @Tibo我需要將結果輸出爲PDF或HTML格式。 HTML給出了不同的結果。所以我有1種方法 - PDF。所以需要讓它工作 – heron 2012-02-12 12:02:42

    回答

    4

    這裏是一個HTML/CSS PDF轉換庫http://www.mpdf1.com/mpdf/

    這有它自己的HTML/CSS語法分析器,因而會產生在所有的瀏覽器相同的結果。

    <?php 
    
    $html = ' 
        <html> 
        <head> 
        <style> 
         table { 
         width: 100%; 
          border-collapse: collapse; 
         }  
         tr { 
    
         } 
         td { 
          width: 38mm; 
          height: 21.2mm; 
          margin: 0 1mm; 
          text-align: center; 
          vertical-align:middle; 
         } 
        </style> 
        </head> 
        <body> 
        <table>'; 
    
        for ($i = 0; $i < 13; $i++) 
        { 
         $html .= '<tr>'; 
         for($j = 0; $j < 5; $j++) 
         { 
          $html .= '<td><barcode code="TEC-IT" type="C39" class="barcode" /></td>'; 
         } 
         $html .= '</tr>'; 
        }  
    
    $html .= '</table> 
        </body> 
        </html>'; 
    
        include("MPDF53/mpdf.php"); 
    
        $mpdf = new mPDF('c', 'A4', '', '', 4, 4, 10.7, 10.7, 0, 0); 
    
        $mpdf->SetDisplayMode('fullpage'); 
    
        $mpdf->list_indent_first_level = 0; 
    
        $mpdf->WriteHTML($html,0); 
    
        $mpdf->Output('test.pdf','I'); 
        exit; 
    
    ?> 
    
    +0

    TCPDF也支持html。看看http://www.tcpdf.org/examples/example_061.phps – heron 2012-02-12 12:23:38

    +0

    是的,但這個更完整(它實際上建立在fpdf上),我在上一個項目中唯一能做的就是改變它div的不透明度。 – redmoon7777 2012-02-12 12:42:48

    +0

    TCPDF中還有一件事情,它支持代碼39條形碼。反正,如果您有使用pdf輸出的經驗,請幫助您使用mpdf或任何pdf文檔進行此項工作。 – heron 2012-02-12 12:49:33

    2

    我認爲,如果你沒有得到的結果在所有的,你可能會被抑制你不能你與你的腳本讓他們的方式錯誤。另外,你使用HTML的方法並不是它的工作原理,您不能使用HTML交織TCPDF本地單元調用;他們不是「輸出」標記。所以你要混合兩種不同的,不兼容的格式,這兩種格式將會分到兩個不同的緩衝區。

    但是,您的代碼仍應該生成PDF。

    請注意最後一頁,帶有標記生成的內容。

    我所做的唯一變化就是將它在那裏我可以不運行的訪問到您的數據:

    $i = 0; 
    //$item = new item($db); 
    //foreach ($_POST['checkbox'] as $id) { 
    for ($id = 0; $id < 1; $id++) { 
        //$details = $item->getDetails($id); 
        //$qt = (isset($_POST['qt'])) ? $_POST['qt'] : $details['qt']; 
        $details = array('These are details'); 
        $qt = 50; 
        for ($cnt = 1; $cnt <= $qt; $cnt++) { 
         // check if it's the beginning of a new table 
         if ($i % 65 == 0) 
          echo '<table>'; 
    
         // check if it's the beginning of a new row 
         if ($i % 5 == 0) 
          echo '<tr>'; 
    
         echo '<td><div class="cell">www.fety.fr<br/>'; 
         $pdf->Cell(0, 0, 'CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9', 0, 1); 
         $pdf->write1DBarcode($id, 'C39', '', '', '', 18, 0.4, $style, 'N'); 
         $pdf->Ln(); 
         echo '<br/>' . $details['hcode'] . '</div></td>'; 
    
         // check if it's the end of a row 
         if (($i + 1) % 5 == 0) 
          echo '</tr>'; 
    
         // check if it's the end of a table 
         if (($i + 1) % 65 == 0) 
          echo '</tr></table>'; 
    
         $i++; 
        } 
    } 
    

    我得到一個PDF。它看起來不像你在圖像中有什麼,但它確實產生了PDF。我看你的代碼是類似於此示例在TDPDF現場約90%:

    http://www.tcpdf.org/examples/example_027.phps

    當我走進去,做我自己的例子,我能得到一個PDF通常模仿大家展示一下在照片裏。正如你將在下面的代碼中看到的那樣,你的可以使用本地TCPDF單元方法來使條形碼生成工作。這並不難;花了我大約30分鐘來弄清楚如何製作pdf。

    我無法弄清楚的唯一原因是頂部的黑線來自哪裏;它以某種方式與標題相關聯,但我無法找到將其關閉的位置。這是第二個PDF後面的代碼:

    <?php 
    
    require_once('tcpdf/config/lang/eng.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 auto page breaks 
    $pdf->SetAutoPageBreak(TRUE); 
    
    //set image scale factor 
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 
    
    //set some language-dependent strings 
    $pdf->setLanguageArray($l); 
    
    $pdf->SetFont('helvetica', '', 10); 
    
    // define barcode style 
    $style = array(
        'position' => '', 
        'align' => 'L', 
        'stretch' => true, 
        'fitwidth' => false, 
        'cellfitalign' => '', 
        'border' => true, 
        'hpadding' => 'auto', 
        'vpadding' => 'auto', 
        'fgcolor' => array(0,0,0), 
        'bgcolor' => false, //array(255,255,255), 
        'text' => true, 
        'font' => 'helvetica', 
        'fontsize' => 8, 
        'stretchtext' => 4 
    ); 
    
    for ($o = 0; $o < 5; $o++) { 
        $pdf->AddPage(); 
    
        $y = 10; 
    
        for ($i = 0; $i < 13; $i++) { 
         $x = 10; 
    
         for ($p = 0; $p < 5; $p++) { 
          // UPC-E 
          $pdf->write1DBarcode('04210000526', 'UPCE', $x, $y, 37, 20, 0.4, $style); 
    
          $x = $x + 38; 
         } 
    
         $y = $y + 21; 
    
         $pdf->Ln(); 
        } 
    } 
    
    //Close and output PDF document 
    $pdf->Output('example_027.pdf', 'I'); 
    
    ?> 
    
    +0

    Thx非常多。我解決了所有與mpdf有關的問題。正在測試。如果它會失敗,我會測試你的方法。 – heron 2012-02-12 15:30:41

    +0

    @Jared Farrish:我有同樣的問題,出現在頂部的黑線。 (你怎麼樣)設法解決這個問題? – Connum 2012-04-06 13:15:52

    +0

    @Connum - 我從來沒有真正知道它,我只是把它放在很難看到它的地方。 – 2012-04-06 18:08:11

    0

    這裏是爲PDF看起來酷似圖像的鏈接:

    Link to embrasse-moi.com barcode label file

    ,這裏是創建它的功能。我使用tcpdf。我創建了3行文本,id,description,price,我從我的sub-id中查找,以便代碼必須替換。我還傳入行/列偏移量以便與avery5167一起使用,以便我們可以使用所有貼紙。

    function printProductLabelsAvery5167($product_sub_ids, $quantities, $row_offset, $column_offset, $filename) 
    { 
        // Embrasse-moi.com 
        require_once('../../../3rdParty/tcpdf/config/lang/eng.php'); 
        require_once('../../../3rdParty/tcpdf/tcpdf.php'); 
    
        $pdf_file_name = $filename; 
    
        $subid = array(); 
        $poc_title = array(); 
        $color_price = array(); 
    
        for($i=0;$i<sizeof($product_sub_ids);$i++) 
        { 
         $pos_product_sub_id = $product_sub_ids[$i]; 
         $pos_product_id = getProductIdFromProductSubId($pos_product_sub_id); 
         for($qty=0;$qty<$quantities[$i];$qty++) 
         { 
          $subid[] = getProductSubIDName($pos_product_sub_id); 
          $poc_title[] = substr(getProductTitle($pos_product_id),0,48); 
          $color_price[] = substr(getProductSubIdColorDescription($pos_product_sub_id),0,38) . '  $' . number_format(getProductRetail($pos_product_id),2); 
         } 
        } 
    
        $margin_left = 0; 
        $margin_right = 0; 
        $margin_top = 0; 
        $margin_bottom = 0; 
        $cell_width = 1.75; 
        $cell_height = 0.5; 
        $cell_spacing = 0.3; 
        $columns = 4; 
        $rows = 20; 
        $line_spacing_adjust = 0.015; 
        $barcode_spacing_adjust = 0.1; 
        $barcode_height_adjust = 0.05; 
    
        $title = 'Avery 5167 template'; 
        $subject = 'PO # 123'; 
        $keywords = 'purchase order 123'; 
        $page_orientation = 'P'; 
        $page_format = 'LETTER'; 
        $unit = 'in'; 
    
        // create new PDF document 
        $pdf = new TCPDF($page_orientation, $unit, $page_format, true, 'UTF-8', false); 
        // set document information 
        $pdf->SetCreator(COMPANY_NAME); 
        $pdf->SetAuthor(getUserFullName($_SESSION['pos_user_id'])); 
        $pdf->SetTitle($title); 
        $pdf->SetSubject($subject); 
        $pdf->SetKeywords($keywords); 
        $pdf->setPrintHeader(false); 
        $pdf->setPrintFooter(false); 
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); 
        $pdf->SetMargins($margin_left, $margin_top, $margin_right); 
        $pdf->SetAutoPageBreak(TRUE, $margin_bottom); 
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 
        $pdf->setLanguageArray($l); 
        $preferences = array('PrintScaling' => 'None'); 
        $pdf->setViewerPreferences($preferences); 
        $pdf->SetFont('helvetica', 'R', 5); 
    
        //barcode: 128a? 
        // define barcode style 
        $barcode_style = array(
        'position' => '', 
        'align' => 'C', 
        'stretch' => false, 
        'fitwidth' => false, 
        'cellfitalign' => '', 
        'border' => false, 
        'hpadding' => '0', 
        'vpadding' => '0', 
        'fgcolor' => array(0,0,0), 
        'bgcolor' => false, //array(255,255,255), 
        'text' => false, 
        'font' => 'helvetica', 
        'fontsize' => 4, 
        'stretchtext' => 0 
    ); 
    
    
        // set border width 
        $pdf->SetLineWidth(0.01); 
        $pdf->SetDrawColor(0,0,0); 
        //$pdf->setCellHeightRatio(3); 
        $counter = 0; 
    
        //calculating the pages.... how many labels are to be printed on the sheet... 
        //how many labels are going on the first sheet? 
        $first_page_number_of_spots = ($rows)*($columns-($column_offset-1)) -($row_offset-1); 
        $number_of_labels = sizeof($subid); 
        if($number_of_labels <= $first_page_number_of_spots) 
        { 
         $pages = 1; 
        } 
        else 
        { 
         $lables_on_first_page = $first_page_number_of_spots; 
         $labels_remaining = $number_of_labels -$lables_on_first_page; 
         $number_of_spots_per_page = $rows*$columns; 
         $pages = ceil($labels_remaining/($number_of_spots_per_page)) + 1; 
        } 
        for($page=0;$page<$pages;$page++) 
        { 
         $pdf->AddPage(); 
         for($col=$column_offset-1;$col<$columns;$col++) 
         { 
          for($row=$row_offset-1;$row<$rows;$row++) 
          { 
           if($counter< sizeof($subid)) 
           { 
            //barcodes must be cap 
            $line1 = strtoupper($subid[$counter]); 
            $line2 = $poc_title[$counter]; 
            $line3 = $color_price[$counter]; 
           } 
           else 
           { 
            $line1 = ''; 
            $line2 = ''; 
            $line3 = ''; 
           } 
           $counter++; 
           $x_spot = $cell_spacing + $col*$cell_width + $col*$cell_spacing; 
           $y_spot = $cell_height + $row*$cell_height; 
           $coords = 'X:'.$x_spot . ' Y:' .$y_spot; 
           $border = 0; 
           $border2 = 0; 
           //this is the cell that will allow allignment to sticker checking 
           $pdf->SetXY($x_spot, $y_spot); 
           $pdf->Cell($cell_width, $cell_height, '', $border, 0, 'C', 0, '', 0, false, 'T', 'M'); 
    
           // CODE 128 A 
           $pdf->SetXY($x_spot+$barcode_spacing_adjust, $y_spot); 
           //cell to check the barcode placement 
           $pdf->Cell($cell_width-2*$barcode_spacing_adjust, $cell_height/2, '', $border, 0, 'C', 0, '', 0, false, 'T', 'M'); 
           $pdf->write1DBarcode($line1, 'C128A', $x_spot+$barcode_spacing_adjust, $y_spot+$barcode_height_adjust, $cell_width-2*$barcode_spacing_adjust, $cell_height/2 - $barcode_height_adjust, 0.4, $barcode_style, 'N'); 
    
           //the remaining 3 lines have to fit in 1/2 the sticker size 
           //$y_offset = $cell_height/2; 
           $pdf->SetXY($x_spot, $y_spot - 0*$line_spacing_adjust + 3/6*$cell_height); 
           $pdf->Cell($cell_width, $cell_height/6, $line1, $border2, 0, 'C', 0, '', 0, false, 'T', 'C'); 
           $pdf->SetXY($x_spot, $y_spot - 1*$line_spacing_adjust + 4/6*$cell_height); 
           $pdf->Cell($cell_width, $cell_height/6, $line2, $border2, 0, 'C', 0, '', 0, false, 'T', 'C'); 
           $pdf->SetXY($x_spot, $y_spot -2*$line_spacing_adjust + 5/6*$cell_height); 
           $pdf->Cell($cell_width, $cell_height/6, $line3, $border2, 0, 'C', 0, '', 0, false, 'T', 'C'); 
    
    
           //$pdf->writeHTMLCell($cell_width, $cell_height, $x_spot, $y_spot, $text_for_label, 1, 1, false, true, '', false); 
           // no work $pdf->MultiCell($cell_width, $cell_height, $text_for_label, 1, 'J', false, '','',true, 0, false, true, $cell_height, 'T', false); 
          } 
          $row_offset = 1; 
         } 
         $column_offset = 1; 
        } 
        //Close and output PDF document 
        $pdf->Output($pdf_file_name, 'D'); 
    
        //============================================================+ 
        // END OF FILE 
        //============================================================+ 
        }