cortexa8
Goto Top

PDF generieren mit JavaScript

Hallo zusammen,
ich habe ein "kleines" Problem:
Ich habe versucht ein PDF-Dokument im Browser zu erstellen. das ganze hat soweit auch gut geklappt, bis ich dynamische Inhalte hinzufügen wollte.
Ich habe makepdf.js genutzt.
Den Content bekomme ich aus einer XML.
Hier ist mein Code:

 
<html>
<head>
<title> TestPDF </title>
<script src='build/pdfmake.js'></script>  
<script src='build/vfs_fonts.js'></script>  
<script>
function getDate(){
                               var today = new Date();
                               var dd = today.getDate();
                               var mm = today.getMonth()+1;
                               var mm = today.getMonth()+1;
                               var yyyy = today.getFullYear();
 
                               if(dd<10) {
                               dd='0'+dd  
                                               }
 
                                               if(mm<10) {
                                                               mm='0'+mm  
                                                               }
 
                                                               today = dd+'/'+mm+'/'+yyyy;  
                                                               return today;
                }
 
function loadXMLDoc(filename) {
    xhttp=new XMLHttpRequest();
                xhttp.open("GET",filename,false);  
                xhttp.send();
                return xhttp.responseXML;
                // Gibt das XML-Objekt zurück
}
 
function loadXMLDoc(filename) {
    xhttp=new XMLHttpRequest();
                xhttp.open("GET",filename,false);  
                xhttp.send();
                return xhttp.responseXML;
}
 
function test() {
 
//return "{ style: 'table', table: { headerRows: 1, widths: [40, 120, 200, 85, 30], body: [[{ text: 'Item', style: 'tableHeader' }, { text: 'Part Number', style: 'tableHeader'}, { text: 'Description', style: 'tableHeader'}, { text: 'Stor. Bin / WO', style: 'tableHeader'}, { text: 'QTY', style: 'tableHeader'}],[{ text: '100', alignment: 'center'}, 'MTC6-8585-BTO', 'Ship To: DLC104836D -- Symbol Technologies, INC.', '336182', { text: '3', alignment: 'center'}],]}}"; 
 
}
 
function makeTable() {
                var xmlDoc=loadXMLDoc("sample.xml");  
                var counterpos = xmlDoc.getElementsByTagName("POSITION").length; // sample.xml -- 21  
                var output = new Array(counterpos);
                var vk = ([ '100', 'lesung', 'termin', '&uuml;', '&auml;'], [ '101', 'lesung', 'termin', '101', '101']);  
                for(var i = 0; i < counterpos; i++) {
                               var item = xmlDoc.getElementsByTagName("POSITION")[i].getAttribute("item") ;  
                               var partnumber = xmlDoc.getElementsByTagName("PN")[i].childNodes.nodeValue;  
                               var descrition = xmlDoc.getElementsByTagName("NAME")[i].childNodes.nodeValue ;  
                               var category = xmlDoc.getElementsByTagName("POSITION")[i].getAttribute("category");  
                               if(category == "ZTM9" || category == "ZTM5") {  
                                               var binwo = xmlDoc.getElementsByTagName("POSITION")[i].getAttribute("wo");  
                               }
                               if(category == "ZTN2") {  
                                               var binwo = xmlDoc.getElementsByTagName("BIN")[i].childNodes.nodeValue ;  
                               }
                               if(category == "TAX") {  
                                               var binwo = " ";  
                               }
                               var quantity = xmlDoc.getElementsByTagName("QTY")[i].childNodes.nodeValue ;  
                              
                               //return [ item, partnumber, descrition, binwo, quantity] ;
                               //output[i] = [ item, partnumber, descrition, binwo, quantity] ;
                               output[i] = "[" + [ item, partnumber, descrition, binwo, quantity] + "]" ;  
 
                               }
                //var stroutput = output.toString();
                //[{ text: '100', alignment: 'center'}, 'MTC6-8585-BTO', 'Ship To: DLC104836D -- Symbol Technologies, INC.', '336182', { text: '3', alignment: 'center'}], 
                return output;  // Alles Values für die Table
               
                return output[1];  // Dieses Array kann ausgeben werden!!! 
               
                //return "[ '100', 'MTC6-8585-BTO', 'Ship To: DLC104836D -- Symbol Technologies, INC.', '336182', '3' ]" ; 
               
}
 
function creationPDF() {
 
                var xmlDoc=loadXMLDoc("sample.xml");  
                var counterpos = xmlDoc.getElementsByTagName("POSITION").length; // sample.xml -- 21  
               
                alert( makeTable() );
                //alert(counterpos);
                var docDefinition = {
                footer: function(currentPage, pageCount) {
                // Footer
                // Contents: Page Count, Print date
                return { text: 'Page ' + currentPage.toString() + ' of ' + pageCount + ' | Print Date: ' + getDate() , alignment: 'center', marginTop: -20};  
               
                },
    header: function(currentPage, pageCount) {
    //Header
    //Contents: A-DloG Logo, Basic Information(SO, Coustumer), Title
    // return {  text: 'A-DloG Picking List\n Sales Order: 7005500', style: 'header' }; 
                //return { image: 'adlog.jpg'       }; 
                return {text: 'A-DloG Picking List', alignment: 'center', fontSize: 20, color: 'red' };  
                //return { image: 'adlog.jpg'       }; 
    },
               
                content: [
                                                               '\n',  
                                                              
                                                                                              {             
                                                                                              table: {
                                                                                                                             widths: [350, 150],
                                                                                                                             body: [
                                                                                                                                                             ['Sold To: DLC104836 -- Symbol Technologies Inc.', 'Sales Order: 7005976'],  
                                                                                                                                                             ['Ship To: DLC104836D -- Symbol Technologies, INC.', 'Due Date: 05/06/2014']                                                                                                                                                   
                                                                                                                             ]
                                                                                                              }
                                                                                              },            
                                                              
                                                               '\n \n',  
                                                                              {             
                                                                                              table: {
                                                                                                                             widths: [100, 397],
                                                                                                                             height: [200, 200],
                                                                                                                             body: [
                                                                                                                                                             [ 'Note', '\n \n \n \n']                                                                                                                                                                                                                                                                                                               
                                                                                                                             ]
                                                                                              }
                                                                              },            
                                                               '\n \n',  
                                                               {
                                                              
                                                                                              style: 'table',  
                                                                                              table: {
                                                                                                                             //headerRows: 1,
                                                                                                                             widths: [40, 120, 200, 85, 30],
                                                                                                                             body: [
                                                                                                                                                             [{ text: 'Item', style: 'tableHeader' }, { text: 'Part Number', style: 'tableHeader'}, { text: 'Description', style: 'tableHeader'}, { text: 'Stor. Bin / WO', style: 'tableHeader'}, { text: 'QTY', style: 'tableHeader'}],  
                                                                                                                                                            
                                                                                                                                                             //[{ text: '100', alignment: 'center'}, 'MTC6-8585-BTO', 'Ship To: DLC104836D -- Symbol Technologies, INC.', '336182', { text: '3', alignment: 'center'}], 
                                                                                                                                                            
                                                                                                                                                             //[{ text: '100', alignment: 'center'}, 'MTC6-8585-BTO', 'Ship To: DLC104836D -- Symbol Technologies, INC.', '336182', { text: '3', alignment: 'center'}], 
                                                                                                                                                             ['110', 'MTC6-8585-BTO', 'Ship To: DLC104836D -- Symbol Technologies, INC.', '336182', '3'],  
                                                                                                                                                             //test()
                                                                                                                                                             //makeTable()
                                                                                                                                                             //test()
                                                                                                                             ]
                                                                                              } // No Layout, Standart Table
                                                               }
                ],
                styles: {
                               header: {
                                              
                                               bold: true,
                                               margin: [40, 0, 6, 100]
                               },
                               footer: {
                                               fontSize: 16,
                                               bold: true,
                                               margin: [20, 10, 0, 5]
                               },
                               subheader: {
                                               fontSize: 16,
                                               bold: true,
                                               margin: [0, 10, 0, 5]
                               },
                               headerinfo: {
                               margin: [40, 0, 6, 10]
                               },
                               table: {
                                               margin: [0, 0, 0, 50] // Left, Top, X ,
                              
                               },
                               tableHeader: {
                                               bold: true,
                                               alignment: 'center',  
                                               fontSize: 13,
                                               color: 'black'  
                               }
                }
};
 
//pdfMake.createPdf(docDefinition).open();
pdfMake.createPdf(docDefinition).download('SO7005500.pdf');  
 
}
</script>
</head>
<body>
 
<a href="javascript:creationPDF()" class="button">Create Picking List</a>  

 
</body>
</html>

Leider kann ich maximal einen Wert returnen.
Eine Schleife ist auch nicht möglich, wenn doch bitte sagen ;)
Ich hab alles mögliche probiert und muss mich jetzt an euch wenden. Hat jemand schon damit gearbeitet bzw. kennt jemand einen Lösung für mein Problem ?
Bitte keine Posts wie : "Warum nicht jspdf", es sei denn er hat ein Beispiel, das besser läuft als meins ;)
Ich hatte bei jspdf deutlich mehr Probleme...

Vielen Dank schonmal im Voraus.

Content-Key: 242522

Url: https://administrator.de/contentid/242522

Ausgedruckt am: 19.03.2024 um 02:03 Uhr

Mitglied: bytecounter
bytecounter 03.07.2014 um 09:41:34 Uhr
Goto Top
Hallo,

hast Du Dir den Code mal nach dem Copy&Paste angeschaut? Es gibt nach allgemeinen Richtlinien eine Längenbegrenzung. Bei der PDF-Erzeugung ist es ja fast unmöglich zu erkennen, welche Klammer wohin gehört.

Des Weiteren kann ich aus dem Code nicht das Problem erkennen? Return kann nur einen Wert zurückgeben. Der Trick: Ein Array..

Meine Glaskugel sagt mir jetzt, dass Du von Zeile 76 / 78 redest. Allerdings finde ich nicht den Punkt, an dem Du diese Funktion überhaupt aufrufst...
Hier der Trick: Kommentare...

vg
Bytecounter
Mitglied: CortexA8
CortexA8 06.07.2014 um 14:36:05 Uhr
Goto Top
Hi,
ja ist mir auch aufgefallen, tut ma wirklich Leid, ich entschlacke den Code mal ;)
Danke schonmal für´s Interesse :D

LG CortexA8
Mitglied: CortexA8
CortexA8 07.07.2014 um 10:31:42 Uhr
Goto Top
Hi,
also ich habe eine Alternative gefunden/erstellt:
Ich hab einfach eine HTML-Tablle genommen auf DIN A4 angepasst und per IE8 ausgedruckt.
Der footer kommt ja automatisch (Seiten + Datum). Den Kopf muss man unter Druckeinstellungen noch anpassen.
Also an alle die etwas ähnliches vorhaben, dies ist die einfachere Lösung !

MfG CortexA8