Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7493699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:07:15+00:00 2026-05-29T17:07:15+00:00

Short What I want to do is, to prepare PHP generated result for print

  • 0

Short

What I want to do is, to prepare PHP generated result for print with strong rules.

Tried all possible ways with css+html: set dimensions in px, mm, cm.
Nothing helped. Each browser, even each printer printed absolutely different paper results (tried both with & w/o border print. also didn’t get result). After long research, found that CSS is not best way for this purpose and better way – to use pdf creation functionality with PHP. So, installed TCPDF. But can’t get it work with logical part that I created for HTML output.

What I want to get

  • Table’s first and last rows’ margin from top and bottom sides of paper must be 11 mm
  • Margin between rows’ 0 mm
  • Table’s rows must be at 4 mm from left and right sides of paper
  • 2 mm between every column
  • 38 mm width x 21.2 mm height each cell
  • 13 x rows, 5 x columns, 13×5=65 cells
  • Each table in new page. In other words – after each table page break
  • In each cell Code 39 Barcode (value must be $id)
  • Only tables in PDF result – no header, no footer, no title … etc

Here is more detailed explanation on image:

enter image description here

What I’m getting

After form submission, processing in php side takes too long – about minute and opens blank page instead of PDF result.

Code:

(Code is not so huge, comments are making it look like so:)

<?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');
?>

Script works like that:

  1. User selects items’ checkboxes
  2. After form submission, PHP gets values of checked checkboxes via Ajax
  3. In foreach loop, PHP gets quantities of each item from database.
  4. Generated table
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:07:17+00:00Added an answer on May 29, 2026 at 5:07 pm

    here is an html/css to pdf converter library http://www.mpdf1.com/mpdf/

    This has it’s own html/css parser and thus will yield the same result in all browser.

    <?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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In short: I want to monitor selected calls from an application to a DLL.
In short: I want to have two fullscreen views, where I can switch between
Short version: I want to trigger the Form_Load() event without making the form visible.
SHORT DESCRIPTION OF PROBLEM: I want to set the text of a searchbar without
Short version: assuming I don't want to keep the data for long, how do
I want to make a long list short by hiding some elements in long
I want to make a short URL service for 2 million assets but I
I want to display dates in the format: short day of week, short month,
I want to test whether a certain string is contained in a short list
In short, I want to use an object literal to allow me to pass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.