jsPDF and jsBarcode - bigktmbig/MyExperienceAlgorithm GitHub Wiki
-
js
$(document).ready(function() { $ ("#pdfDownloader").click(function() {var pdf = new jsPDF('p', 'pt', 'a4'); pdf.deletePage(1);
var pdfName = 'test.pdf';
var options = {split: false}; var $divs = $('.renderMe'); var numRecursionsNeeded = $divs.length - 1; var currentRecursion=0; function recursiveAddHtmlAndSave(currentRecursion, totalRecursions){ if(currentRecursion==totalRecursions){ pdf.save(pdfName); }else{ currentRecursion++; pdf.addPage(250,350); pdf.fromHTML($('.renderMe')[currentRecursion], 15, 20, options, function(){ console.log(currentRecursion); recursiveAddHtmlAndSave(currentRecursion, totalRecursions) }); } } recursiveAddHtmlAndSave(currentRecursion, numRecursionsNeeded);
}); })
*html
- resolve 2: *html
- js
$(document).ready(function() { $ ("#pdfDownloader").click(function() {
var pdf = new jsPDF('p', 'mm', 'a4'); pdf.deletePage(1);
var pdfName = 'test.pdf';
var options = {split: false};
var
function recursiveAddHtmlAndSave(currentRecursion, totalRecursions){ if(currentRecursion==totalRecursions){ pdf.save(pdfName); }else{ currentRecursion++; pdf.addPage(70,22); pdf.fromHTML($('.renderMe')[currentRecursion], 0, 0, options, function(){ console.log(currentRecursion); recursiveAddHtmlAndSave(currentRecursion, totalRecursions) }); } }
recursiveAddHtmlAndSave(currentRecursion, numRecursionsNeeded);
}); })
========================================================================== *js $scope.exportData = function(){ var pdf = new jsPDF('p', 'mm', 'a4'); pdf.deletePage(1);
var pdfName = 'barcode.pdf';
const options = {
pagesplit: false
};
const productDivs = $('#export_barcode').find('.products');
const totalProduct = productDivs.length;
for (let countProduct = 0; countProduct <= totalProduct; countProduct++) {
if(countProduct == totalProduct) {
pdf.save(pdfName);
}else {
const renderDivs = productDivs.eq(countProduct).find('.render');
const itemDivs = productDivs.eq(countProduct).find('.item-center');
const svg = productDivs.eq(countProduct).find('svg');
const pro_name = productDivs.eq(countProduct).find('.pro-name');
const pro_code = productDivs.eq(countProduct).find('.pro-code');
const pro_price = productDivs.eq(countProduct).find('.pro-price');
var qty_col = itemDivs.length;
var currentRecursion = -1;
const page_height = $scope.barcode_size?(1*$scope.barcode_size.height):22;
const page_width = $scope.barcode_size?(1*$scope.barcode_type*$scope.barcode_size.width):35;
const div_width = $scope.barcode_size?(1*$scope.barcode_size.width):35;
var numRecursionsNeeded = renderDivs.length;
const page_bt = $scope.barcode_type;
const per_tmp = $scope.barcode_size.fontSize*3.779528/2;
const add_tmp = $scope.barcode_size.fontSize*4/3;
const font_tmp = $scope.barcode_size.fontSize*3.779528;
function isASCII(text, extended) {
return (extended ? /^[\x00-\xFF]$/ : /^[\x00-\x7F]$/).test(text);
}
function escapeTextforPdf(text) {
text = text.toUpperCase();
for(var i=0;i<text.length;++i) {
if(!isASCII(text[i], true)) {
text = text.replace(RegExp(text[i], "g"), text[i].normalize('NFD').replace(/[\u0300-\u036f]/g, ""));
}
}
return text;
}
function recursiveAddHtmlAndSave(currentRecursion, totalRecursions){
currentRecursion++;
if(currentRecursion==totalRecursions){
console.log(123);
}else{
pdf.addPage(page_width,page_height);
pdf.setFontSize(font_tmp);
var tmp_padding = (page_height - (pro_name[currentRecursion].clientHeight+svg[currentRecursion].clientHeight+pro_code[currentRecursion].clientHeight+pro_price[currentRecursion].clientHeight))/7;
var padding_spacing = tmp_padding;
var padding_top = 2*tmp_padding<2?2:tmp_padding;
var margin_left_name = (div_width-(pdf.getStringUnitWidth(pro_name[currentRecursion].textContent)*per_tmp))/2;
var margin_left_bc = (div_width-svg[currentRecursion].clientWidth)/2;
var margin_left_code = (div_width-(pdf.getStringUnitWidth(pro_code[currentRecursion].textContent)*per_tmp))/2+add_tmp;
var margin_left_price = (div_width-(pdf.getStringUnitWidth(pro_price[currentRecursion].textContent)*per_tmp))/2+add_tmp;
var height_name = padding_top+pro_name[currentRecursion].clientHeight;
var height_bc = height_name+padding_spacing;
var height_code = height_bc+svg[currentRecursion].clientHeight+pro_code[currentRecursion].clientHeight+padding_spacing;
var height_price = height_code+pro_price[currentRecursion].clientHeight+padding_spacing;
if(margin_left_name<0) {
margin_left_name = add_tmp;
}
var items_full = parseInt(qty_col/page_bt*1);
var items_rest = parseInt(qty_col-(page_bt*items_full));
if(currentRecursion < items_full){
var height_tmp = page_height*(currentRecursion-1);
var width_tmp = 0;
for (var j_f = 0; j_f < page_bt*1; j_f++) {
width_tmp = div_width*j_f;
pdf.text(escapeTextforPdf(pro_name[currentRecursion].textContent), margin_left_name+width_tmp, height_name);
svg2pdf(svg[currentRecursion], pdf, {
xOffset: margin_left_bc+width_tmp,
yOffset: height_bc,
width: svg[0].clientWidth,
height: svg[0].clientHeight,
scale: 1
});
pdf.text(escapeTextforPdf(pro_code[currentRecursion].textContent), margin_left_code+width_tmp, height_code);
pdf.text(escapeTextforPdf(pro_price[currentRecursion].textContent), margin_left_price+width_tmp, height_price);
}
}
if (items_rest!=0 && currentRecursion == items_full) {
var width_tmp = 0;
for (var i_r = 0; i_r < items_rest; i_r++) {
width_tmp = div_width*i_r;
pdf.text(escapeTextforPdf(pro_name[currentRecursion].textContent), margin_left_name+width_tmp, height_name);
svg2pdf(svg[currentRecursion], pdf, {
xOffset: margin_left_bc+width_tmp,
yOffset: height_bc,
width: svg[0].clientWidth,
height: svg[0].clientHeight,
scale: 1
});
pdf.text(escapeTextforPdf(pro_code[currentRecursion].textContent), margin_left_code+width_tmp, height_code);
pdf.text(escapeTextforPdf(pro_price[currentRecursion].textContent), margin_left_price+width_tmp, height_price);
}
}
recursiveAddHtmlAndSave(currentRecursion, totalRecursions);
}
}
recursiveAddHtmlAndSave(currentRecursion, numRecursionsNeeded);
}/*end if check count product*/
}/*end for product*/
}
- html
-
js add list all $scope.add_list_all = function (bs, bt) { var length_s = $scope.arr_add_services?$scope.arr_add_services.length:0; for (var i = 0; i < length_s; i++) {
if($scope.arr_add_services[i].checked == true) { /*add row, column*/ $scope.arr_add_services[i].arr_row = []; var items_full = parseInt($scope.arr_add_services[i].qty_pr/bt*1); var items_rest = parseInt($scope.arr_add_services[i].qty_pr-(bt*items_full)); for (var i_f = 0; i_f < items_full; i_f++) { $scope.arr_add_services[i].arr_row[i_f] = []; for (var j_f = 0; j_f < bt*1; j_f++) { $scope.arr_add_services[i].arr_row[i_f].push({}); } } if (items_rest!=0) { $scope.arr_add_services[i].arr_row[items_full] = []; for (var i_r = 0; i_r < items_rest; i_r++) { $scope.arr_add_services[i].arr_row[items_full].push({}); } } /*push to print*/ $scope.arr_print_services.push($scope.arr_add_services[i]); } } $scope.change_size(bs); $scope.change_type(bt); $scope.arr_add_services = []; $ngBootbox.hideAll(); }