I need to draw a horizontal dotted line in pdf using TCPDF. I tried:
$style = array('width' => 0.1, 'cap' => 'butt', 'join' => 'miter', 'dash' => 1, 'color' => array(0, 0, 0));
$pdf->Line(5, 50, 100, 50, $style);
I used the dash parameter. When I set it to 1 it draws short dashes, but I need dots. I did not find an explanation of the style parameters or any manual on the internet.
Please use the below code for the draw horizontal dotted line in pdf using TCPDF and apply a style to it.
<?php
require_once('tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// disable header and footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// set font
$pdf->SetFont('helvetica', '', 10);
// add a page
$pdf->AddPage();
//set 'width' => 0.1, 'dash' => '1,1,1,1' as per your requirement
$style = array('width' => 0.5, 'dash' => '2,2,2,2', 'phase' => 0, 'color' => array(255, 0, 0));
// Line
$pdf->Text(5, 4, 'Line examples');
$pdf->Line(5, 20, 200, 20, $style);
//Close and output PDF document
$pdf->Output('line_example.pdf', 'I');
I hope this helps!
You can have dots, if you want to.
Just set the cap to round in the style properties:
$style = array('width' => 0.5, 'cap => 'round', 'dash' => '2,2,2,2', 'phase' => 0, 'color' => array(255, 0, 0));
Related
I'm struggling with some additional margins
$pageLayout = [
$width,
$height,
];
$pdf = new TCPDF('L, 'mm', $pageLayout, TRUE, 'UTF-8', FALSE);
$pdf->setCellPaddings(0,0,0,0);
$textBorder = array('LTRB' => array('width' => 0.001, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
$pdf->Text($paddingLR, $paddingTB,$myText,0,false,true,$textBorder,0,'L',false,'',0,false,'A','C',true);
The border is only for visualisation.
Preview of rendered Text
now i'm surprised about the margins around the text. I'd expect, that the Text starts within the border line. The same for the space above the letters.
Does anyone have a suggestion how to make this work?
I'm creating a PDF file from a source file that is 119.88 x 50.058 mm. However, when i place a QR code, I cannot make it bigger whether i specify it with size 50 or 500, no different. What do i need to do to make the QR code bigger?
Here is my code
$pdf = new FPDI('L', 'mm', array('119.888','50.038'));
$pdf->setPrintHeader(false);
$pdf->AddPage();
$page = $pdf->setSourceFile('aw_print.PDF');
$page = $pdf->ImportPage(1, 'TrimBox');
$pdf->useTemplate($page, 0, 0);
$x = 10;
$y = 10;
$style = array(
'border' => 1,
'vpadding' => 'auto',
'hpadding' => 'auto',
'fgcolor' => array(0,0,0),
'bgcolor' => false, //array(255,255,255)
'module_width' => 1, // width of a single module in points
'module_height' => 1 // height of a single module in points
);
$pdf->write2DBarcode('www.tcpdf.org', 'QRCODE,H', 10, 10, 60, 60, $style, 'N');
$pdf->Output('test.PDF', 'D');
Result of size 60
https://prnt.sc/i8yva3
Result of size 500
https://prnt.sc/i8yvkt
$pdf = new FPDI('L', 'mm', array('119.888','50.038'));
$pdf->setPrintHeader(false);
Add SetAutoPageBreak and set it to false. This will make sure that style works as expected.
$pdf->SetAutoPageBreak(false); // important so styles don't break
$pdf->AddPage();
$page = $pdf->setSourceFile('aw_print.PDF');
$page = $pdf->ImportPage(1, 'TrimBox');
$pdf->useTemplate($page, 0, 0);
$x = 10;
$y = 10;
$style = array(
'border' => 1,
'vpadding' => 'auto',
'hpadding' => 'auto',
'fgcolor' => array(0,0,0),
'bgcolor' => false, //array(255,255,255)
'module_width' => 1, // width of a single module in points
'module_height' => 1 // height of a single module in points
);
By default a barcode is confined to the page and margin size.
Adding true for distort will enable you to scale to what ever size.
// write2DBarcode($code, $type, $x, $y, $w, $h, $style, $align, $distort) {
$pdf->write2DBarcode('www.tcpdf.org', 'QRCODE,H', 10, 10, 60, 60, $style, 'N', true);
$pdf->Output('test.PDF', 'D');
Now that you are able to scale past the margins and page size style will not render properly unless you followed the first step and added SetAutoPageBreak.
I'm trying to add some barcodes programmatically in a pdf file but I think I'm not understanding something correctly regarding positioning.Here's my code
$pdf = new TCPDF("L", "mm", array(80, 40), true, 'UTF-8', false);
$pdf->SetMargins(3, 3, 3);
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->setFontSubsetting(true);
$pdf->SetFont('freeserif', '', 12);
foreach ($barcodes as $barcode){
$pdf->AddPage();
$x = $pdf->GetX();
$y = $pdf->GetY();
$style = array(
'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
);
//$pdf->Cell(30, 1, 'EAN 13', 0, 1);
$pdf->write1DBarcode($barcode, 'EAN13', '5', '6', '60', 14, 0.4, $style, 'N');
}
The result I get is like the image or in any way not even close to outputting the barcode where I'd like it to be and I have no clue why. I'd appreciate any help
if i use
$pdf->write1DBarcode($barcode, 'EAN13', '5', '5', '60', 14, 0.4, $style, 'N');
There is no empty pdf page but the barcode is still really high. I'd like it to go like 1/3 from top around 14mm that's why I was trying to fiddle with the y variable in write1DBarcode
After lots of fiddling around I found it.
Adding
$pdf->SetAutoPageBreak(TRUE, 0);
Made the autobreak behave correctly
My guess is that a bottom margin set by default that was messing around the alignment vertically that can't be set using $pdf->SetMargins. In any case problem solved for me. Thanks for trying to help
Use Below code
<?php $params = $pdf->serializeTCPDFtagParameters(array('5027010247321', 'C39', '', '', 80, 30, 0.4, array('position'=>'S', 'border'=>true, 'padding'=>4, 'fgcolor'=>array(0,0,0), 'bgcolor'=>array(255,255,255), 'text'=>true, 'font'=>'helvetica', 'fontsize'=>8, 'stretchtext'=>4), 'N'));
?>
And paste these below line where you want tcpdf baarcode
<tcpdf method="write1DBarcode" params="<?php echo $params; ?>" />
Visit [https://www.youtube.com/watch?v=flXFPbKRmUo][2]
$style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(255, 0, 0));
$pdf->Line(5, 10, 80, 30, $style);
I uses the above statements to draw a line.
Result view in screen has this line drawn.
However It's not printed on paper.
Any help would be highly appreciated.
I'm sorry folks, it's not TCPDF problem.
I was using Google to print out the PDF generated and having this problem.
If I try Internet Explorer there is no problem.
I have created a custom header for my PDF created with TCPDF. Now I would like to add a blue line (about 2px width) that goes across the page at the bottom of the header but can't figure out how?
I believe you do it like this:
$style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(255, 0, 0));
$pdf->Line(5, 10, 80, 30, $style);
Here is the full example
http://www.tcpdf.org/examples/example_012.phps
I found the easiest way to put line
$pdf->writeHTML("<hr>", true, false, false, false, '');
You can also use the page axis:
$pdf->Line(5, $pdf->y, $pdf->w - 5, $pdf->y);
However if you are trying to render a colored <hr> html tag you will need to adjust the TCPDF::DrawColor (this excerpt is from code that adds a graph bar to each row of a data report as per $twidth and $lengthmm):
$htmlbar = '<hr style="width:' . $lengthmm . 'mm;">';
$oldDrawColor = $pdf->DrawColor;
$pdf->setDrawColor(121, 161, 46);
$pdf->MultiCell($twidth,'2',$htmlbar,0,'L',$fill,1,'','',true,0,true,false,4,'T',false);
$pdf->DrawColor = $oldDrawColor;
Drawing a horizontal black line at the current position:
$style = ['width' => 0.2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => [0, 0, 0]];
$this->pdf->SetLineStyle($style);
$this->pdf->Line(PDF_MARGIN_LEFT, $this->pdf->getY(), $this->pdf->getPageWidth()-PDF_MARGIN_LEFT, $this->pdf->getY());
$this->pdf->Ln();
The point is to get the x value for the second point.
This is how I do it:
$pageWidth = $pdf->getPageWidth(); // Get total page width, without margins
$pageMargins = $pdf->getMargins(); // Get all margins as array
$headerMargin = $pageMargins['header']; // Get the header margin
$px2 = $pageWidth - $headerMargin; // Compute x value for second point of line
$p1x = $this->getX();
$p1y = $this->getY();
$p2x = $px2;
$p2y = $p1y; // Use same y for a straight line
$style = array();
$this->Line($p1x, $p1y, $p2x, $p2y, $style);
Links
TCPDF::getMargins()
http://www.tcpdf.org/doc/code/classTCPDF.html#ae9bd660bf5b5e00eea82f1168cc67b5b
TCPDF::getPageWidth()
http://www.tcpdf.org/doc/code/classTCPDF.html#a510ab21d6a373934bcd3bd4683704b7e
Have fun!
Just add some HTML : )
$html ='<hr>';
$pdf->writeHTML($html, true, false, true, false, '');