How can I make the images position to the top and doc.text no worked - jspdf

how to let image and the 'Hello World' text show at the top, the table but the doc.text doesn't work in the pdf.
const doc = new jspdf();
var imgData2 = 'data:image/jpeg;base64,/verylongtextcode';
doc.addImage(imgData2, "PNG", 10, 78, 150, 50);
doc.text("Hello World", 15, 20);
doc.autoTable({ html: "#mytable" });
doc.save("table.pdf");

Related

JSPDF Autotable not exporting defined table footer

I have an html table defined based on dynamic data. The table contains a thead, tfoot and tbody. The tfoot is mapped to specific values within my json. However, when using JSPDF Autotable and exporting to PDF the footer is not rendered. I have seen information but no code examples of the showfoot option and have tried it after the doc.autotable, and even after the style options but to no avail. The footer is not exported. I'm sure it super simple - but I can't seem to figure it out. Note: I do not want JSDPF Autotable to 'create' a footer - it is defined, it is part of my table - I simply want it rendered on the pdf. I found an old stackoverflow from 2016 where this was mentioned - Simon B. commented it would be added - the topic was closed - but I couldn't find a code example anywhere.
Here is my jspdf autotable code where I have tried to 'show my footer' - but to no avail. Any assistance appreciated.
<script>
function generate() {
//Creation of PDF document
let doc = new jsPDF('l', 'pt');
const totalPagesExp = '{total_pages_count_string}';
var elem = document.getElementById('${pm.info.requestId}');
var data = doc.autoTableHtmlToJson(elem);
doc.autoTable(data.columns, data.rows, {
headStyles: {
cellWidth: 'wrap',
fontSize: 10,
lineWidth: 0,
lineColor: [0, 0, 0],
textColor: [0, 0, 0],
fillColor: [255,255,255]
},
bodyStyles: {
cellWidth: 'wrap',
fontSize: 8,
lineWidth: 0,
lineColor: [0, 0, 0],
textColor: [0, 0, 0],
fillColor: [255,255,255]
},
footStyles: {
cellWidth: 'wrap',
fontSize: 10,
lineWidth: 0,
lineColor: [0, 0, 0],
textColor: [0, 0, 0],
fillColor: [211,211,211]
},
//Formatting of pages
didDrawPage: function (data) {
//Summa logo on top of the page
doc.addImage('${pm.variables.get("summa")}', 'PNG', 20, 20, 145, 42.63);
//Font sizes of report information
doc.setFontSize(8);
//Report information: portfolio name, knowledge time and report time
doc.text(35, 75, '${pm.variables.get("portfolioName")}');
doc.text(35, 85, '${pm.variables.get("reportTime")}');
doc.text(35, 95, '${pm.variables.get("knowledgeTime")}');
//Page numbers
var str = "Page " + doc.internal.getNumberOfPages()
if (typeof doc.putTotalPages === 'function') {
str = str + " of " + totalPagesExp;
};
//Page size
var pageSize = doc.internal.pageSize;
var pageHeight = pageSize.height ? pageSize.height : pageSize.getHeight();
doc.text('Theme "plain"', 14, 16);
},
margin: {
top: 100
},
});
//Number of pages
if (typeof doc.putTotalPages === 'function') {
doc.putTotalPages(totalPagesExp);
},
//--------------------------------------------------------------------------------------------------START
//Change name of report if desired
doc.save('${pm.info.requestName}${pm.variables.get("reportTime")}.pdf');
//--------------------------------------------------------------------------------------------------END
}
Not sure if you already managed to export footer totals to the jspdf. I got it to working, with a small adjustment.
Replace the var elem = document.getElementById('${pm.info.requestId}'); to var elem = document.getElementById('NAME');
And replace the doc.autoTable(data.columns, data.rows, { to doc.autoTable({html: '#NAME',

Getting trouble in the jsPdf auto-table Custom width Columns

Hi all Am trying to Increase my Specified column size Column size , So i used like this {columnStyles: {xxx: {columnWidth: 18}}} , Column size was increased but next lines's are getting affected . That mean's next line is theme: 'grid', mean's it's not working . Can someone clarify me pls . Here is My code ,
doc.autoTable(getColumns(), getData(), //{columnStyles: {steps: {columnWidth: 65}}},
{
theme: 'grid', // 'striped', 'grid' or 'plain'
headerStyles: {
fillColor: [189, 200, 255],
textColor: [12, 1, 1]
},
// margin: { top: 50, left: 20, right: 20, bottom: 0 },
styles: {
overflow: 'linebreak',
columnWidth: 110,
lineWidth: 2,
lineColor: [85, 51, 27]
},
pageBreak: 'avoid',
beforePageContent: function(data) {
doc.setFontSize(10);
doc.text("Process Name :"+mainData.name+" || "+"Description :"+mainData.description, 20, 15);
},
//startY: doc.autoTableEndPosY() + 20,
columnStyles: {
//0: {columnWidth: 200}
}
});
//doc.save("xxx.pdf");
Try changing the pdfsize , below are the formats
pageFormats = { // Size in pt of various paper formats
'a0': [2383.94, 3370.39], 'a1': [1683.78, 2383.94],
'a2': [1190.55, 1683.78], 'a3': [841.89, 1190.55],
'a4': [595.28, 841.89], 'a5': [419.53, 595.28],
'a6': [297.64, 419.53], 'a7': [209.76, 297.64],
'a8': [147.40, 209.76], 'a9': [104.88, 147.40],
'a10': [73.70, 104.88], 'b0': [2834.65, 4008.19],
'b1': [2004.09, 2834.65], 'b2': [1417.32, 2004.09],
'b3': [1000.63, 1417.32], 'b4': [708.66, 1000.63],
'b5': [498.90, 708.66], 'b6': [354.33, 498.90],
'b7': [249.45, 354.33], 'b8': [175.75, 249.45],
'b9': [124.72, 175.75], 'b10': [87.87, 124.72],
'c0': [2599.37, 3676.54], 'c1': [1836.85, 2599.37],
'c2': [1298.27, 1836.85], 'c3': [918.43, 1298.27],
'c4': [649.13, 918.43], 'c5': [459.21, 649.13],
'c6': [323.15, 459.21], 'c7': [229.61, 323.15],
'c8': [161.57, 229.61], 'c9': [113.39, 161.57],
'c10': [79.37, 113.39], 'dl': [311.81, 623.62],
'letter': [612, 792],
'government-letter': [576, 756],
'legal': [612, 1008],
'junior-legal': [576, 360],
'ledger': [1224, 792],
'tabloid': [792, 1224],
'credit-card': [153, 243]
};
You have to say as var pdfsize = 'a1';

Word wrap in generated PDF (using jsPDF)?

what I'm doing is using jsPDF to create a PDF of the graph I generated. However, I am not sure how to wrap the title (added by using the text() function). The length of the title will vary from graph to graph. Currently, my titles are running off the page. Any help would be appreciated!
This is the code i have so far:
var doc = new jsPDF();
doc.setFontSize(18);
doc.text(15, 15, reportTitle);
doc.addImage(outputURL, 'JPEG', 15, 40, 180, 100);
doc.save(reportTitle);
Nothing to keep the reportTitle from running off the page
Okay I've solved this. I used the jsPDF function, splitTextToSize(text, maxlen, options). This function returns an array of strings. Fortunately, the jsPDF text() function, which is used to write to the document, accepts both strings and arrays of strings.
var splitTitle = doc.splitTextToSize(reportTitle, 180);
doc.text(15, 20, splitTitle);
You can just use the optional argument maxWidth from the text function.
doc.text(15, 15, reportTitle, { maxWidth: 40 });
That will split the text once it reaches the maxWidth and start on the next line.
Auto-paging and text wrap issue in JSPDF can achieve with following code
var splitTitle = doc.splitTextToSize($('#textarea').val(), 270);
var pageHeight = doc.internal.pageSize.height;
doc.setFontType("normal");
doc.setFontSize("11");
var y = 7;
for (var i = 0; i < splitTitle.length; i++) {
if (y > 280) {
y = 10;
doc.addPage();
}
doc.text(15, y, splitTitle[i]);
y = y + 7;
}
doc.save('my.pdf');
To wrap long string of text to page use this code:
var line = 25 // Line height to start text at
var lineHeight = 5
var leftMargin = 20
var wrapWidth = 180
var longString = 'Long text string goes here'
var splitText = doc.splitTextToSize(longString, wrapWidth)
for (var i = 0, length = splitText.length; i < length; i++) {
// loop thru each line and increase
doc.text(splitText[i], leftMargin, line)
line = lineHeight + line
}
If you need to dynamically add new lines you want to access the array returned by doc.splitTextToSize and then add more vertical space as you go through each line:
var y = 0, lengthOfPage = 500, text = [a bunch of text elements];
//looping thru each text item
for(var i = 0, textlength = text.length ; i < textlength ; i++) {
var splitTitle = doc.splitTextToSize(text[i], lengthOfPage);
//loop thru each line and output while increasing the vertical space
for(var c = 0, stlength = splitTitle.length ; c < stlength ; c++){
doc.text(y, 20, splitTitle[c]);
y = y + 10;
}
}
Working Helper function
Here's a complete helper function based on the answers by #KB1788 and #user3749946:
It includes line wrap, page wrap, and some styling control:
(Gist available here)
function addWrappedText({text, textWidth, doc, fontSize = 10, fontType = 'normal', lineSpacing = 7, xPosition = 10, initialYPosition = 10, pageWrapInitialYPosition = 10}) {
var textLines = doc.splitTextToSize(text, textWidth); // Split the text into lines
var pageHeight = doc.internal.pageSize.height; // Get page height, well use this for auto-paging
doc.setFontType(fontType);
doc.setFontSize(fontSize);
var cursorY = initialYPosition;
textLines.forEach(lineText => {
if (cursorY > pageHeight) { // Auto-paging
doc.addPage();
cursorY = pageWrapInitialYPosition;
}
doc.text(xPosition, cursorY, lineText);
cursorY += lineSpacing;
})
}
Usage
// All values are jsPDF global units (default unit type is `px`)
const doc = new jsPDF();
addWrappedText({
text: "'Twas brillig, and the slithy toves...", // Put a really long string here
textWidth: 220,
doc,
// Optional
fontSize: '12',
fontType: 'normal',
lineSpacing: 7, // Space between lines
xPosition: 10, // Text offset from left of document
initialYPosition: 30, // Initial offset from top of document; set based on prior objects in document
pageWrapInitialYPosition: 10 // Initial offset from top of document when page-wrapping
});
When we use linebreak in jsPDF we get an error stating b.match is not defined, to solve this error just unminify the js and replace b.match with String(b).match and u will get this error twice just replace both and then we get c.split is not defined just do the same in this case replace it with String(c).match and we are done. Now you can see line breaks in you pdf. Thank you

Unable to create a transparent QFrame

I want to create QWidget which has no background; and I want to add a transparent QFrame to it. This is my code:
QWidget *change_impl_win = new QWidget();
QHBoxLayout *mainLayout = new QHBoxLayout(change_impl_win);
QVBoxLayout *vbox_l = new QVBoxLayout();
QFrame *back_frame = new QFrame();
back_frame->setBackgroundColor(QColor(125, 125, 125, 125));
QHBoxLayout *frame_hbox = new QHBoxLayout(back_frame);
frame_hbox->addLayout(vbox_l);
mainLayout->addWidget(back_frame);
QGroupBox *newImplant = new QGroupBox("");
QGridLayout *layoutNewImplant = new QGridLayout;
newImplant->setLayout(layoutNewImplant);
vbox_l->addWidget(newImplant);
QRadioButton *cb_ob_rb = new QRadioButton("Up", change_impl_win);
QRadioButton *cb_un_rb = new QRadioButton("Down", change_impl_win);
layoutNewImplant->addWidget(cb_ob_rb);
layoutNewImplant->addWidget(cb_un_rb);
QLabel* lengthLabel = new QLabel("Length:");
QSpinBox *cb_implantLength = new QSpinBox;
cb_implantLength->setRange(1, 20);
cb_implantLength->setSingleStep(1);
cb_implantLength->setSuffix(" mm");
layoutNewImplant->addWidget(lengthLabel, 2, 0);
layoutNewImplant->addWidget(cb_implantLength, 2, 1);
QLabel* diameterLabel = new QLabel("Diameter:");
QSpinBox *cb_implantDiameter = new QSpinBox;
cb_implantDiameter->setRange(1, 20);
cb_implantDiameter->setSingleStep(1);
cb_implantDiameter->setSuffix(" mm");
layoutNewImplant->addWidget(diameterLabel, 3, 0);
layoutNewImplant->addWidget(cb_implantDiameter, 3, 1);
QPushButton *cb_apply_prop = new QPushButton();
cb_apply_prop->setText("Apply");
cb_apply_prop->setGeometry(400, 0, 200, 40);
cb_apply_prop->setFont(QFont("Charter",13));
cb_apply_prop->setAutoFillBackground(true);
frame_hbox->addWidget(cb_apply_prop);
change_impl_win->setAttribute(Qt::WA_TranslucentBackground);
change_impl_win->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
change_impl_win->show();
But, I can't see the transparent QFrame. I am seeing only controls added to QWidget with no background. Please tell me where I made mistake. Thank you.

alignX isn't working?

Here's my simple code; I'm expecting my atom1 widget (row:0, column:0) to be righ aligned; but can't get it! Any suggestions?
var layout = new qx.ui.layout.Grid();
layout.setRowFlex(0, 1); // make row 0 flexible
layout.setColumnWidth(1, 200); // set with of column 1 to 200 pixel
var container = new qx.ui.container.Composite(layout);
this.getRoot().add(container, {left:200, top:200});
var atom1 = new qx.ui.basic.Atom("Icon Right1", "").set({alignX:'right', alignY:'middle'});
var atom2 = new qx.ui.basic.Atom("Icon Right2", "");
var button1 = new qx.ui.form.Button("First Button", "test/test.png");
container.add(atom1, {row: 0, column: 0});
container.add(atom2, {row: 0, column: 1});
container.add(button1, {row: 1, column: 0});
you have to prevent the atom from growing or it will just fill the cell and have no room to move.
add allowGrowX: false ...
see the qooxdoo playground for an example

Resources