@@ -21,114 +21,104 @@ fetchFile(testImageURL)
21
21
console . error ( error ) ;
22
22
} ) ;
23
23
24
- function makePDF ( PDFDocument , lorem , waitForData , iframe ) {
25
- // create a document
26
- var doc = new PDFDocument ( ) ;
27
-
28
- doc . registerFont ( 'Roboto' , 'fonts/Roboto-Regular.ttf' ) ;
29
-
30
- // draw some text
31
- doc . fontSize ( 25 ) . text ( 'Here is some vector graphics...' , 100 , 80 ) ;
32
-
33
- // some vector graphics
34
- doc
35
- . save ( )
36
- . moveTo ( 100 , 150 )
37
- . lineTo ( 100 , 250 )
38
- . lineTo ( 200 , 250 )
39
- . fill ( '#FF3300' ) ;
40
-
41
- doc . circle ( 280 , 200 , 50 ) . fill ( '#6600FF' ) ;
42
-
43
- // an SVG path
44
- doc
45
- . scale ( 0.6 )
46
- . translate ( 470 , 130 )
47
- . path ( 'M 250,75 L 323,301 131,161 369,161 177,301 z' )
48
- . fill ( 'red' , 'even-odd' )
49
- . restore ( ) ;
50
-
51
- // and some justified text wrapped into columns
52
- doc
53
- . font ( 'Roboto' )
54
- . text ( 'And here is some wrapped text...' , 100 , 300 )
55
- . fontSize ( 13 )
56
- . moveDown ( )
57
- . text ( lorem , {
58
- width : 412 ,
59
- align : 'justify' ,
60
- indent : 30 ,
61
- columns : 2 ,
62
- height : 300 ,
63
- ellipsis : true
64
- } ) ;
65
-
66
- doc . addPage ( ) ;
67
-
68
- doc
69
- . fontSize ( 25 )
70
- . font ( 'Courier' )
71
- . text ( 'And an image...' )
72
- . image ( 'images/bee.png' ) ;
73
-
74
- doc . font ( 'Courier-Bold' ) . text ( 'Finish...' ) ;
75
-
76
- doc . addPage ( ) ;
77
-
78
- doc
79
- . font ( 'Roboto' )
80
- . fontSize ( 18 )
81
- . text ( 'Not yet. Lets try to show an image lazy loaded' ) ;
24
+ var initialFnCode = `// create a document
25
+ var doc = new PDFDocument();
26
+
27
+ doc.registerFont('Roboto', 'fonts/Roboto-Regular.ttf');
28
+
29
+ // draw some text
30
+ doc.fontSize(25).text('Here is some vector graphics...', 100, 80);
31
+
32
+ // some vector graphics
33
+ doc
34
+ .save()
35
+ .moveTo(100, 150)
36
+ .lineTo(100, 250)
37
+ .lineTo(200, 250)
38
+ .fill('#FF3300');
39
+
40
+ doc.circle(280, 200, 50).fill('#6600FF');
41
+
42
+ // an SVG path
43
+ doc
44
+ .scale(0.6)
45
+ .translate(470, 130)
46
+ .path('M 250,75 L 323,301 131,161 369,161 177,301 z')
47
+ .fill('red', 'even-odd')
48
+ .restore();
49
+
50
+ // and some justified text wrapped into columns
51
+ doc
52
+ .font('Roboto')
53
+ .text('And here is some wrapped text...', 100, 300)
54
+ .fontSize(13)
55
+ .moveDown()
56
+ .text(lorem, {
57
+ width: 412,
58
+ align: 'justify',
59
+ indent: 30,
60
+ columns: 2,
61
+ height: 300,
62
+ ellipsis: true
63
+ });
82
64
83
- try {
84
- doc . image ( 'images/test.jpg' ) ;
85
- } catch ( error ) {
86
- doc . moveDown ( ) . text ( `${ error } ` ) ;
87
- doc . text ( 'Image not loaded. Try again later.' ) ;
88
- }
65
+ doc.addPage();
66
+
67
+ doc
68
+ .fontSize(25)
69
+ .font('Courier')
70
+ .text('And an image...')
71
+ .image('images/bee.png');
89
72
90
- // waitForData must be called before call to doc.end()
91
- waitForData ( doc )
92
- . then ( dataUrl => {
93
- // display the document in the iframe to the right
94
- iframe . src = dataUrl ;
95
- } )
96
- . catch ( error => {
97
- console . log ( error ) ;
98
- } ) ;
99
-
100
- doc . end ( ) ;
73
+ doc.font('Courier-Bold').text('Finish...');
74
+
75
+ doc.addPage();
76
+
77
+ doc
78
+ .font('Roboto')
79
+ .fontSize(18)
80
+ .text('Not yet. Lets try to show an image lazy loaded');
81
+
82
+ try {
83
+ doc.image('images/test.jpg');
84
+ } catch (error) {
85
+ doc.moveDown().text(\`\${error}\`);
86
+ doc.text('Image not loaded. Try again later.');
87
+ }
88
+
89
+ // waitForData must be called before call to doc.end()
90
+ waitForData(doc)
91
+ .then(dataUrl => {
92
+ // display the document in the iframe to the right
93
+ iframe.src = dataUrl;
94
+ })
95
+ .catch(error => {
96
+ console.log(error);
97
+ });
98
+
99
+ doc.end();` ;
100
+
101
+ function executeFn ( code , PDFDocument , lorem , waitForData , iframe ) {
102
+ var fn = new Function ( 'PDFDocument' , 'lorem' , 'waitForData' , 'iframe' , code ) ;
103
+ fn ( PDFDocument , lorem , waitForData , iframe ) ;
101
104
}
102
105
103
106
var editor = ace . edit ( 'editor' ) ;
104
107
editor . setTheme ( 'ace/theme/monokai' ) ;
105
108
editor . getSession ( ) . setMode ( 'ace/mode/javascript' ) ;
106
- editor . setValue (
107
- makePDF
108
- . toString ( )
109
- . split ( '\n' )
110
- . slice ( 1 , - 1 )
111
- . join ( '\n' )
112
- . replace ( / ^ / gm, '' )
113
- ) ;
109
+ editor . setValue ( initialFnCode ) ;
114
110
editor
115
111
. getSession ( )
116
112
. getSelection ( )
117
113
. clearSelection ( ) ;
118
114
119
115
var iframe = document . querySelector ( 'iframe' ) ;
120
- makePDF ( PDFDocument , lorem , waitForData , iframe ) ;
116
+
117
+ executeFn ( initialFnCode , PDFDocument , lorem , waitForData , iframe ) ;
121
118
122
119
editor . getSession ( ) . on ( 'change' , function ( ) {
123
120
try {
124
- var fn = new Function (
125
- 'PDFDocument' ,
126
- 'lorem' ,
127
- 'waitForData' ,
128
- 'iframe' ,
129
- editor . getValue ( )
130
- ) ;
131
- fn ( PDFDocument , lorem , waitForData , iframe ) ;
121
+ executeFn ( editor . getValue ( ) , PDFDocument , lorem , waitForData , iframe ) ;
132
122
} catch ( e ) {
133
123
console . error ( e ) ;
134
124
}
0 commit comments