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

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

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

Highcharts bar chart with fixed bar widths and gaps

I've set
pointWidth: 20,
pointPadding: 0,
groupPadding: 0
But the chart still expands to fill the default chart height. Is it possible to have Highcharts adjust the chart height instead?
http://jsfiddle.net/MLELM/
The chart will fill the available height with what you give it.
point and group padding are a proportion of the available space, so the chart is honoring the properties as specified.
It uses the size of the containing element to determine its size, so it cannot possibly ignore the height of the containing element.
If you want a smaller chart that fills a larger space on your page, you can put your containing element in another wrapper element. Then you can calculate yourself how tall your chart should be based on the number of series, and pass that height to the container.
If you want to have the series that you have specified clustered near the top, you will need to add additional null data points to fill the space below them.
If you can be more specific about what you want and why, better answers may be available.
[[ update
Stumbled across this post again, and remembered that I have a demo that automatically sets the chart height based on some predefined parameters, and the number of data points.
var barCount = chartData.length,
pointWidth = 20,
marginTop = 70,
marginRight = 10,
marginBottom = 50,
marginLeft = 100,
groupPadding = 0,
pointPadding = 0.3,
chartHeight = marginTop
+ marginBottom
+ ((pointWidth * barCount)
* (1 + groupPadding + pointPadding));
Fiddle here:
http://jsfiddle.net/jlbriggs/kpu5d1qf/
Related question here:
How to calculate the height of bar chart
Yes, you can set the chart height like that:
chart: {
type: 'bar',
height: 200,
},
See also http://jsfiddle.net/MLELM/3/

Zoom-In and Zoom-Out can not resize?

I'm using MFC Print preview dialog. Before I used BMP image to represent those zoom buttons and they worked fine, but now I want to use the text labeling instead. But no matter what value I use, the size remains the same when I run the program. Here below what I have:
IDD_FILE_KHANH_PRINT_PREVIEW DIALOG 0, 0, 219, 19
STYLE DS_SETFONT | WS_CHILD
FONT 8, "MS Sans Serif"
BEGIN
PUSHBUTTON "&Print",AFX_ID_PREVIEW_PRINT,2,3,36,12
PUSHBUTTON "Zoom &In",AFX_ID_PREVIEW_ZOOMIN,40,3,46,50
PUSHBUTTON "Zoom &Out",AFX_ID_PREVIEW_ZOOMOUT,70,3,46,12
PUSHBUTTON "Pre&v Page",AFX_ID_PREVIEW_PREV,100,3,46,12
PUSHBUTTON "&Next Page",AFX_ID_PREVIEW_NEXT,150,3,46,12
CONTROL "Landscape",IDC_LANDSCAPE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,230,3,67,13,WS_EX_CLIENTEDGE
PUSHBUTTON "&Close",AFX_ID_PREVIEW_CLOSE,300,3,35,13
END
SO I change the values for two buttons, and one is working while zoom-in does not work, for example:
PUSHBUTTON "&Print",AFX_ID_PREVIEW_PRINT,2,3,136,12
this one works because i can see the Print button has wider length. So I change the value from 36 to 136.
PUSHBUTTON "Zoom &In",AFX_ID_PREVIEW_ZOOMIN,40,3,146,50
But this one does not work because the ZoomIn remains the same size and here I change 46 to 146.
I believe this is only the place that I assign the values for the buttons. thanks.
The height of your "Zoom &In" button is 50 but the dialog height itself is only 19. You should change the the height of the button so it is 12 just like the others.
It seems I made a mistake. Before I used bitmap image to represent those buttons and when I decide to use text labeling and forgot this function below that loads the bitmap of the zooming buttons. So after commenting out the m_zoomIn and m_zoomOut statements, they work now.
int CKhanhPrintPreview::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CPreviewView::OnCreate(lpCreateStruct) == -1)
return -1;
m_pToolBar->EnableToolTips( TRUE );
m_zoomIn.AutoLoad(AFX_ID_PREVIEW_ZOOMIN, m_pToolBar, IDB_PREV_ZOOMIN );
m_zoomOut.AutoLoad(AFX_ID_PREVIEW_ZOOMOUT, m_pToolBar, IDB_PREV_ZOOMOUT );
return 0;
}
thanks.

Resources