Page 1 of 3
PDF generator (jsPDF)
Posted: Thu Apr 10, 2014 12:38 am
by eddy
- In this sample, I'm loading dynamically a JS script (
jsPDF plugin :
https://github.com/MrRio/jsPDF)
- this plugin provides some PDF functions. (preview, generate, export, convert from HTML)
Remark: I didn't use the minified version of this plugin because there's a conflict when SpiderBasic is in debug mode.
Code: Select all
Procedure CloseWindowEvent()
CloseWindow(EventWindow())
EndProcedure
Procedure ScriptLoaded()
DisableGadget(1,#False)
EndProcedure
Procedure LoadScript(script$, *func)
!$.getScript(v_script$,p_func);
EndProcedure
Procedure GadgetEvents()
Select EventGadget()
Case 1
;save PDF
Protected txt$=GetGadgetText(20)
!var doc = new jsPDF();
!doc.text(20, 20, v_txt$); //write text
!doc.setDrawColor(0);
!doc.setFillColor(255, 0, 0);
!doc.rect(100, 140, 10, 110, 'F'); // filled red square
!doc.setLineWidth(2);
!doc.line(20, 240, 60, 230); // line
!doc.addPage(); //add new page
!doc.setFontSize(24);
!doc.setFontType("italic");
!doc.setTextColor(0,255,0);
!doc.text(20, 20, 'jsPDF plugin');
!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())
CompilerIf #PB_Compiler_OS <> #PB_OS_Web
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
CompilerEndIf
EndIf
LoadScript("https://rawgit.com/MrRio/jsPDF/master/dist/jspdf.debug.js",@ScriptLoaded()) ;loading plugin asynchronously...
Re: PDF generator (jsPDF)
Posted: Wed Jun 03, 2015 10:23 pm
by JaviCervera
Where does it put the generated "Test.pdf" file? It doesn't seem to save anything on OS X.
Re: PDF generator (jsPDF)
Posted: Fri Jun 05, 2015 8:00 pm
by eddy
JaviCervera wrote:Where does it put the generated "Test.pdf" file? It doesn't seem to save anything on OS X.
It's not an OSX bug. I coded this program with an old beta version of SpiderBasic.
It was broken because of recent naming rule change :
http://forums.spiderbasic.com/viewtopic ... +name#p832
I fixed my code.
Re: PDF generator (jsPDF)
Posted: Sat Jun 06, 2015 11:24 pm
by JaviCervera
Yeah, works wonderfully now. Thank you!
Re: PDF generator (jsPDF)
Posted: Wed Nov 01, 2017 8:46 am
by MarkOtt
The actual jsPDF version 1.3 does not work with Spiderbasic 2.2
The last working version i found is jsPDF 1.2.61 , but it displays images with poor quality.
It can be found at:
https://github.com/MrRio/jsPDF/releases
For online linking at:
https://cdnjs.cloudflare.com/ajax/libs/ ... f.debug.js
Many greetings. Markus
Re: PDF generator (jsPDF)
Posted: Wed Nov 01, 2017 10:33 am
by Peter
MarkOtt wrote:The actual jsPDF version 1.3 does not work with Spiderbasic 2.2
try this one:
Code: Select all
Procedure CloseWindowEvent()
CloseWindow(EventWindow())
EndProcedure
Procedure ScriptLoaded()
DisableGadget(1, #False)
EndProcedure
Procedure GadgetEvents()
Select EventGadget()
Case 1
; save PDF
Protected txt$ = GetGadgetText(20)
!var doc = new jsPDF();
!doc.text(20, 20, v_txt$); //write text
!doc.setDrawColor(0);
!doc.setFillColor(255, 0, 0);
!doc.rect(100, 140, 10, 110, 'F'); // filled red square
!doc.setLineWidth(2);
!doc.line(20, 240, 60, 230); // line
!doc.addPage(); //add new page
!doc.setFontSize(24);
!doc.setFontType("italic");
!doc.setTextColor(0,255,0);
!doc.text(20, 20, 'jsPDF plugin');
!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://rawgit.com/MrRio/jsPDF/master/dist/jspdf.debug.js"], function(jsPDF) {
! window.jsPDF = jsPDF;
ScriptLoaded()
! });
Greetings ... Peter
Re: PDF generator (jsPDF)
Posted: Wed Nov 01, 2017 11:25 am
by falsam
It's a good idea to refresh this topic. Thank.
Re: PDF generator (jsPDF)
Posted: Fri Nov 03, 2017 11:02 am
by MarkOtt
Peter, thank you very much, this works very well.
Here is another example showing the generated PDF in a WebGadget. It works very well in Firefox, but not in IE, Edge, Chrome.
It seems that blob and string data of doc.output() works in Firefox only. Or am I overseeing something?
Code: Select all
Procedure CloseWindowEvent()
CloseWindow(EventWindow())
EndProcedure
Procedure ScriptLoaded()
DisableGadget(1,#False)
Debug "Script loaded."
EndProcedure
Procedure GadgetEvents()
Select EventGadget()
Case 1
Debug "Button pressed."
OpenWindow(99, 200, 200, 600, 600, "Show PDF in WebGadget")
WebGadget(98, 10, 10, 580, 580, "")
BindEvent(#PB_Event_CloseWindow, @CloseWindowEvent())
Protected txt$=GetGadgetText(20)
! var doc = new jsPDF();
! doc.setProperties({
! title: 'PDF Title - - - - - - - - - - - - - - - - - - - ',
! subject: 'Info about PDF',
! author: 'PDFAuthor',
! keywords: 'generated, javascript, web 2.0, ajax',
! creator: 'My Company'
! });
! doc.text(20, 20, v_txt$); //write text
! doc.setDrawColor(0);
! doc.setFillColor(255, 0, 0);
! doc.rect(100, 140, 10, 110, 'F'); // filled red square
! doc.setLineWidth(2);
! doc.line(20, 240, 60, 230); // line
! doc.addPage(); //add new page
! doc.setFontSize(24);
! doc.setFontType("italic");
! doc.setTextColor(0,255,0);
! doc.text(20, 20, 'jsPDF plugin');
; ;save PDF (works in all browsers)
; ! doc.save('Test.pdf');
; ;open PDF in new tab (works in Firefox only ?)
; ! doc.output('dataurlnewwindow');
; ;open PDF in same tab (works in Firefox only ?)
; ! doc.output('datauri');
;open PDF in WebGadget (works in Firefox only ?)
Protected outstring.s = ""
;! v_outstring = doc.output('datauristring'); //works in Firefox only
! v_outstring = doc.output('bloburl'); //works in Firefox only
Debug outstring
SetGadgetText(98, outstring)
! window.URL.revokeObjectURL(v_outstring); //if bloburl
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
;load jsPDF
! require(["https://rawgit.com/MrRio/jsPDF/master/dist/jspdf.debug.js"], function(jsPDF) {
! window.jsPDF = jsPDF;
ScriptLoaded()
! });
Regards. Markus
Re: PDF generator (jsPDF)
Posted: Tue Feb 16, 2021 11:06 am
by Dirk Geppert
Unfortunately the JS link no longer works. Maybe this is the correct project:
https://github.com/MrRio/jsPDF ?
Re: PDF generator (jsPDF)
Posted: Tue Feb 16, 2021 4:48 pm
by Peter
Dirk Geppert wrote:Unfortunately the JS link no longer works.
the current version is 2.3.0. Here is the adjusted code:
Code: Select all
Procedure CloseWindowEvent()
CloseWindow(EventWindow())
EndProcedure
Procedure ScriptLoaded()
DisableGadget(1, #False)
EndProcedure
Procedure GadgetEvents()
Select EventGadget()
Case 1
; save PDF
Protected txt$ = GetGadgetText(20)
!var doc = new jsPDF();
!doc.text(20, 20, v_txt$); //write text
!doc.setDrawColor(0);
!doc.setFillColor(255, 0, 0);
!doc.rect(100, 140, 10, 110, 'F'); // filled red square
!doc.setLineWidth(2);
!doc.line(20, 240, 60, 230); // line
!doc.addPage(); //add new page
!doc.setFontSize(24);
! // doc.setFontType("italic"); doesn't work anymore?
!doc.setTextColor(0,255,0);
!doc.text(20, 20, 'jsPDF plugin');
!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()
! });