WriteHTMLCell doesn't change GetY() - tcpdf

I'm trying to work out the height of a WriteHTMLCell box.. I thought I could use the difference between the Y position before and after calling WriteHTMLCell()....
$start_y = $pdf->GetY();
$pdf->WriteHTMLCell(
$w,
0, // min height
$xpos, // XPos
$ypos, // YPos
$text,
1, // border
0, // ln
false, // fill
false, // reseth
"R"
);
$end_y = $pdf->GetY();
..But $start_y always equals $end_y (NB, the x position does move)
The tcpdf manual Says this.. "After the call, the current position moves to the right or to the next line." ... but it doesn't say why it moves to the right and not to the next line.
NB, I have done extensive research. This question may appear similar to Another stack overflow question - However, this is for a different tcpdf call.

The answer was to change the argument $ln to 1
$pdf->WriteHTMLCell(
$w,
0, // min height
$xpos, // XPos
$ypos, // YPos
$text,
1, // border
1, // ln
false, // fill
false, // reseth
"R"
);

Related

TCPDF create move-able "box" with text without using ln settings

I am trying to create a bit of a unique table in TCPDF as shown below:
Getting the captions, headers and rows setup is easy. The hard part is the "bar".
The bar needs a few things:
Editable X-position
Text
Originally I was trying to use MultiCell; however, I got some weird behavior as shown below:
Multi-Cell:
I believe this is because of the ln settings. If we look at my code you can see that I am trying to create a Cell and then a MultiCell inside of it. Both of these use the have the ln setting to put the next cell below.
I tried setting the MultiCell's ln to 0 (to the right) but it had no visible change.
//multi cell
// extend TCPF with custom functions
class MYPDF extends TCPDF {
// SCC table
public function SCCTable($headers,$rows) {
// Colors, line width and bold font
$this->SetFillColor(0,128,128);
$this->SetTextColor(0);
$this->SetDrawColor(0);
$this->SetLineWidth(0.3);
$this->SetFont('', 'B');
// Header
$num_headers = count($headers);
for($i = 0; $i < $num_headers; ++$i) {
$this->Cell(
$headers[$i]->width,
$headers[$i]->height,
$headers[$i]->text,
1,
0,
'C',
1
);
}
$this->Ln();
// Color and font restoration
$this->SetFillColor(255);
$this->SetTextColor(0);
$this->SetFont('');
// Data
$fill = 0;
$num_rows = count($rows);
for($i = 0; $i < $num_rows; ++$i){
$this->Cell(//Row
$headers[$i]->width * $num_headers, //Row width should be the sum of all header width.
$rows[$i]->height,
//Row's Text
$this->MultiCell(
$rows[$i]->width,
$rows[$i]->height,
$rows[$i]->text,
0, //Border
"C", //Text Align
false, //fill, determines if the background is painted or transparent (false).
2, //ln, 1 = Next cell starts at beginning of new line.
$rows[$i]->x,
$rows[$i]->y
),
1, //Border
2, //ln, 1 = Next cell starts at beginning of new line.
"L" //text align
);
}
}
}
After this I found out about TextField. When I tried this I got just as weird behavior...
//text field
// extend TCPF with custom functions
class MYPDF extends TCPDF {
// SCC table
public function SCCTable($headers,$rows) {
// Colors, line width and bold font
$this->SetFillColor(0,128,128);
$this->SetTextColor(0);
$this->SetDrawColor(0);
$this->SetLineWidth(0.3);
$this->SetFont('', 'B');
// Header
$num_headers = count($headers);
for($i = 0; $i < $num_headers; ++$i) {
$this->Cell(
$headers[$i]->width,
$headers[$i]->height,
$headers[$i]->text,
1,
0,
'C',
1
);
}
$this->Ln();
// Color and font restoration
$this->SetFillColor(255);
$this->SetTextColor(0);
$this->SetFont('');
// Data
$fill = 0;
$num_rows = count($rows);
for($i = 0; $i < $num_rows; ++$i){
$this->Cell(//Row
$headers[$i]->width * $num_headers, //Row width should be the sum of all header width.
$rows[$i]->height,
//Row's Text
$this->TextField(
$rows[$i]->text,
$rows[$i]->width,
$rows[$i]->height,
[],
[],
$rows[$i]->x,
$rows[$i]->y
),
1, //Border
2, //ln, 1 = Next cell starts at beginning of new line.
"L" //text align
);
}
}
}
Finally I thought of using the Rect function to create a rectangle and Text to the draw the text. Using variables I could "glue" the Text to the Rectangle; however, the textfield uses the ln setting as well; furthermore, looking at the actual code there is this line:
$this->Cell(0, 0, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
Seeing as it creates a cell, then it should run into the same problem as MultiCell, as the only difference between Cell and MultiCell in my case is the ability to change the x-position from the left border.
So I'm stuck with this question: How can I draw a "box" that has text and can be pushed along horizontally?
How this is done is not that important except that images aren't an option.
I realized I didn't actually need to have the original row. I could just make due with the "bar".
$this->MultiCell(
$rows[$i]->width,
$rows[$i]->height,
$rows[$i]->text,
1, //Border
"C", //Text Align
true, //fill, determines if the background is painted (true) or transparent (false).
2, //ln, 1 = Next cell starts at beginning of new line.
$rows[$i]->x,
$rows[$i]->y
);
Though with that said it would still be nice to know how to do with rows...

How to align text in center using jspdf

How to align text center using jsPDF.
var doc = new jsPDF();
doc.text(40, 250, 'Hi How are you');
If you are using the latest version (1.1.135) the api has changed some for the text function. It now reads as:
API.text = function(text, x, y, flags, angle, align);
If you don't need to use the flags or angle though, you can simply use:
var doc = new jsPDF();
doc.text('Hi How are you', 40, 250, 'center');
Keep in mind that the center call uses the x parameter now as the center of the text string, and not the left most border as it does when rendering left aligned.
Link to source
Edit:
Alternately you can calculate the proper x offset to just use the text function normally like so:
var text = "Hi How are you",
xOffset = (doc.internal.pageSize.width / 2) - (doc.getStringUnitWidth(text) * doc.internal.getFontSize() / 2);
doc.text(text, xOffset, 250);
Angular 6.
Footer align to horizontally center
var doc = new jsPDF();
var pageHeight = doc.internal.pageSize.height || doc.internal.pageSize.getHeight();
var pageWidth = doc.internal.pageSize.width || doc.internal.pageSize.getWidth();
// FOOTER
let str = "Your footer text";
doc.setTextColor(100);
doc.setFontSize(10);
doc.text(str, pageWidth / 2, pageHeight - 10, {align: 'center'});
doc.save("example.pdf");
Above answers didn't work for me, I ended up doing the following to center the text
let textX = (doc.internal.pageSize.getWidth() - doc.getTextWidth(text))/2
doc.text(text, textX, textY);
this worked:
var xOffset = doc.internal.pageSize.width / 2
doc.text('hello world', xOffset, 8, {align: 'center'});
2022: this works assuming your page width is 210 (default A4).
doc.text("This is centred text.", 105, 80, null, null, "center");
Here's a link to their live demo per the README:
http://raw.githack.com/MrRio/jsPDF/master/index.html
2022: I'm finding that JSPDF is buggy. It took a while to figure out how to install the advertised 'runs in a browser' implementation for a PHP app instead of a JS front end framework. There's a line that's required window.jsPDF = window.jspdf.jsPDF; that isn't mentioned anywhere in the documentation, I had to go through a downloaded example piece by piece to find it. Now I'm finding that the center text function doesn't work. In 2 different local environments and a JSFiddle, on multiple browsers, it sends the text off the left side of the page when the align:center option is implemented. While the above solution works, it breaks down if text is longer than one line, which, incidentally, is another out of the box bug - the text runs out of the document instead of wrapping, and there is no wrap option. So, it seems after all these hours I'm out of luck and I'll have to go a different route. Plugin is not maintained and this should be noted in documentation. Recommend to not waste your time.
This works somewhat, but isn't precise, if you know please tell me why.
I calculate the width of my text in order to center it myself.
For this, I used the getTextDimensions() method on my jsPDF object
var pdf = new jsPDF({
orientation : 'p',
unit: 'px',
format: [500, 750],
putOnlyUsedFonts:true
});
var textDimensions = pdf.getTextDimensions('MyText');
You can now use textDimensions.w to get text-width and textDimensions.h for height
Then use this to center your text.
var textWidth = textDimensions.w;
pdf.text('MyText', (pdfWidth / 2) - (textWidth / 2), 100);
BUT: You need to know your PDF's width to do this.
I 'solved' this by defining height and width myself, but you can easily find height and width of common formats online.
Like A4: 210mm*297mm.
Just remember to set unit: 'mm' when creating your jsPDF.
var doc = new jsPDF();
// set midPage for variable use
var midPage = doc.internal.pageSize.getWidth()/2
// Default is 210 mm so default midway by value is 105
doc.setFontSize(40);
doc.text("Octonyan loves jsPDF", 105, 15, null, null, "center");
// Better to use a variable "midPage" (from above)
doc.setFontSize(30);
doc.text("Centered (USA), Centred (UK)", midPage , 30, null, null, "center");

How can I obtain 2 blocks of 3 columns on the same page with TFPDF

I want to create a layout of 2 blocks on a page, each with 3 columns. I am currently using example 10 of the documentation and the setEqualColumns() method.
How can I fix a maximum height?
The result I want to achieve:
you must use SetAutoPageBreak() and resetColumns() functions (more details here). Be careful, if your text is longer than the allowed space, the rest of the text will be written on the next page.
For example :
//Set the distance from the bottom the first block must stop : 130
$pdf->SetAutoPageBreak(true, 130);
//Write the first block : 3 columns, width 50
$pdf->setEqualColumns(3, 50);
$pdf->writeHTML($content_1);
//reset columns
$pdf->resetColumns();
//reset the X position and the page break
$margins = $pdf->getMargins();
$pdf->setX($margins['left']);
$pdf->SetAutoPageBreak(true, $margins['bottom']);
//write the chapter title (like in the TCPDF example 10)
$pdf->SetFillColor(200, 220, 255);
$pdf->Cell(180, 6, 'Chapter 2', 0, 1, '', 1);
//write the second block
$pdf->setEqualColumns(3, 50);
$pdf->writeHTML($content_2);

Vaadin Alignment with GridLayout to center over cells

I trying to get image and title to be in the center
the login in box works OK
private void createTitle(final AbstractOrderedLayout parentLayout) {
GridLayout layout = new GridLayout(6, 2);
layout.setSpacing(true);
layout.setWidth( 100, Sizeable.Unit.PERCENTAGE );
layout.setHeight( 100, Sizeable.Unit.PERCENTAGE );
parentLayout.addComponent(layout);
parentLayout.setComponentAlignment(layout, Alignment.TOP_CENTER);
Embedded image = new Embedded("", new ClassResource("Icon_25x32.png"));
layout.addComponent(image, 2, 0);
layout.setComponentAlignment(image, Alignment.TOP_CENTER);
Label titleLabel = new Label("YT-100 ATU Control Center");
titleLabel.addStyleName("v-label-largeTitleText");
//layout.addComponent(titleLabel, 3, 1);
layout.addComponent(titleLabel, 3, 1, 4,1);
layout.setComponentAlignment(titleLabel, Alignment.TOP_CENTER);
mUserLayout = new GridLayout(2, 2);
mUserLayout.setSpacing(true);
layout.addComponent(mUserLayout, 5, 0);
layout.setComponentAlignment(mUserLayout, Alignment.MIDDLE_CENTER);
}
why does this not center?
Fixed:
titleLabel.setSizeUndefined();
and
.v-caption-centered, input.centered {
text-align: center;
}
notice that title label has 100% width by default, so it will take the entire width of the containing layout cell. Centering is only meaningful for components that take less width than the containing cell. Hence, you'd need to set the label width to undefined.

How to set the minimum date in achartengine graph

Hi in my app I made this graph !I´ve set renderer.setXAxisMin(0) but the first label of x axis is always hidden even if I try to use negative values the label do not appear. Thanks in advance
graph
My code :
mRenderer.setMargins(new int[] { 30, 25, 20, 45 });
mRenderer.setApplyBackgroundColor(true);
// SET THE BACKGROUND COLOR
getUnits(units);
mRenderer.setBackgroundColor(Color.parseColor("#339999"));
mRenderer.setAxisTitleTextSize(TEXT_SIZE_HDPI);
mRenderer.setChartTitleTextSize(TEXT_SIZE_HDPI);
mRenderer.setLabelsTextSize(16);
// mRenderer.setPointSize(TEXT_SIZE_HDPI);
// mRenderer.setXTitle(getResources().getString(R.string.date));
mRenderer.setXLabelsColor(Color.WHITE);
mRenderer.setYLabelsColor(0, Color.WHITE);
// mRenderer.setYTitle(getResources().getString(R.string.weight));
mRenderer.setFitLegend(true);
mRenderer.setMarginsColor(Color.parseColor("#339999"));
mRenderer.setPanEnabled(true, true);
mRenderer.setScale(1);
mRenderer.setXAxisMin(0);
mRenderer.setInScroll(true);
mRenderer.setShowGridY(false);
mRenderer.setAntialiasing(true);
mRenderer.setZoomEnabled(false, false);
mRenderer.setShowAxes(false);
mRenderer.setShowGrid(false);

Resources