Using Table In Jspdf without HTML

Share your advanced knowledge/code with the community.
loulou2522
Posts: 51
Joined: Wed Mar 18, 2015 5:52 am

Using Table In Jspdf without HTML

Post by loulou2522 »

Here is a skeleton for writing a pdf with table.
Thhis program write a head title on each page and a numbering page at the bottom part of each part. If someone can implemented this work with cells heiht on each row i lwill be pleased.

Code: Select all

Procedure CloseWindowEvent()
  CloseWindow(EventWindow())
EndProcedure

Procedure ScriptLoaded()
  DisableGadget(1, #False)
EndProcedure

Procedure GadgetEvents()
  
  Select EventGadget()     
      
    Case 1
      ShowDebugOutput()
      ;  Protected txt$ = "Bordereau d'envoi Virements"
      
      !var doc = new jsPDF( 'p', 'mm','a4'); 
      ; !doc.setFont("arial narrow");
      !doc.setFontSize(15);
      
      
      ! margins = { top : 80, bottom : 60, left : 60,width : 522 }; 
      
      
      !// Optional - set properties on the document
      !doc.setProperties({
      !title: 'Title',
      !subject: 'Anomalies',
      !author: 'SEPA',
      !keywords: 'erreur, javascript, web 2.0, ajax',
      !creator: 'SPIDERBASIC'
      !});
     !doc.setFillColor(137, 252, 226);
      !doc.setDrawColor(0);
      ; !doc.setLineWidth(1);
      !doc.roundedRect(70, 10, 110, 10,2,2, 'DF'); // filled red square
      !doc.text("Bordereau d'envoi Virements",120, 16, {align: 'center'});  //write text
      ! var generateData = function(amount) {
      ! var result = [];
      ! var data = {
      !  coin: "1000",
      !  game_group: "GameGroup",
      !  game_name: "XPTO2",
      !  game_version: "25",
      !  machine_un: "204858611",
      !  vlt: "0"
      ! };
      ! for (var i = 0; i < amount; i += 1) {
      !  data.id = (i + 1).toString();
      !  result.push(Object.assign({}, data));
      !}
      ! return result;
      !};
      
      !function createHeaders(keys) {
      ! var result = [];
      ! for (var i = 0; i < keys.length; i += 1) {
      ! result.push({
      !  id: keys[i],
      ! name: keys[i],
      ! prompt: keys[i],
      ! width: 65,
     ! align: "center",
     ! padding: 15
      ! });
      !}
      !return result;
      !}
      !var headers = createHeaders([
      ! "id",
      ! "coin",
      ! "game_group",
      ! "game_name",
      ! "game_version",
      ! "machine_un",
      ! "vlt"
      !]);
      ;!var doc = new jsPDF({ putOnlyUsedFonts: true, orientation: "landscape");
      !  doc.table(20, 30, generateData(100), headers, {
      !    margins: {
      !     top: 30,
      !    bottom: 30
      ! },
      ! width: 60,
      ! autoSize: true,
      ! printHeaders: true,
      ! rowMargin: 0,
      ! rowHeight: 12,
      ! headerRowHeight: 10,
      ! fontSize: 11} );
      !  pages = doc.internal.getNumberOfPages();
      ! pageWidth = doc.internal.pageSize.width;  //Optional
      ! pageHeight = doc.internal.pageSize.height;  //Optional
      !for (let j = 1; j < pages + 1 ; j++) {
      !     let horizontalPos = pageWidth / 2;  //Can be fixed number
      !    let verticalPos = pageHeight - 10;  //Can be fixed number
      !   doc.setPage(j);
      !  if (j>1) {
      !doc.setDrawColor(0);
      !doc.setFillColor(137, 252, 226);
      !doc.roundedRect(70, 10, 110, 10,2,2, 'DF'); // filled red square
      !doc.setFontSize(15);
      !doc.text("Bordereau d'envoi Virements",120, 16, {align: 'center'});  //write text
      !doc.setFontSize(12);
      ! }
      !  doc.text("Pages "+j+ '/' + pages, horizontalPos, verticalPos, {align: 'right'});
      !}
      
      !// Then save
      
      
      ; ! demoHTML();
      ;       
      ;       !function demoHTML() {
      ;       !       !var doc = new jsPDF(); 
      ;       !               source = '<html><body><p>test HTML string</p></body></html>';
      ;       ! margins = { top : 80, bottom : 60, left : 60,width : 522 }; 
      ;       !        // all coords And widths are in jsPDF instance's declared units
      ;       !       // 'inches' in this Case
      ;       !      doc.fromHTML(source, // HTML string Or DOM elem ref.
      ;       !     margins.left, // x coord
      ;       !    margins.top, { // y coord
      ;       !       'width' : margins.width, // max width of content on PDF
      ;       !  },
      ;       
      ;       !  function(dispose) {
      ;       !     // dispose: object With X, Y of the last line add To the PDF 
      ;       !    //          this allow the insertion of new lines after html
      ;       !   doc.save('fileNameOfGeneretedPdf.pdf');
      ;       ! }, margins);
      ;       ! });
      ;       
      ;       ! });
      ;       
      ;       
      ;       !};
      ;       
      ;       
      ;       
      ! doc.save("test.pdf")
    Case 2
      
      ; convert HTML To PDF
      
  EndSelect
  
EndProcedure

If OpenWindow(0, 100, 100, 300, 200, "PDF Generator powered by jsPDF")
  
  EditorGadget(20,  5, 5, 280, 150)
  SetGadgetText(20,"jsPDF" + #CRLF$ + "Generate PDF files in client-side JavaScript.")
  ButtonGadget(1, 5, 160,  190, 32, "Export")
  DisableGadget(1, #True)
  
  BindEvent(#PB_Event_Gadget, @GadgetEvents())
  BindEvent(#PB_Event_CloseWindow, @CloseWindowEvent())
  
EndIf



! require(["https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js"], function(jsPDF) {
!window.jsPDF = jsPDF.jsPDF;
ScriptLoaded()
! });
Special Thanks to Peter which help me beginning with spiderbasic