@@ -70,73 +70,70 @@ ReadableSVGStream.prototype._read = function () {
70
70
} ;
71
71
72
72
// Streams the SVG element to the given file path.
73
- function writeSvgToFile ( svgElement , filePath ) {
73
+ async function writeSvgToFile ( svgElement , filePath ) {
74
74
let readableSvgStream = new ReadableSVGStream ( {
75
75
svgElement,
76
76
} ) ;
77
77
const writableStream = fs . createWriteStream ( filePath ) ;
78
- return new Promise ( function ( resolve , reject ) {
79
- readableSvgStream . once ( "error" , reject ) ;
80
- writableStream . once ( "error" , reject ) ;
81
- writableStream . once ( "finish" , resolve ) ;
82
- readableSvgStream . pipe ( writableStream ) ;
83
- } ) . catch ( function ( err ) {
78
+ try {
79
+ await readableSvgStream . once ( "error" ) ;
80
+ await writableStream . once ( "error" ) ;
81
+ await writableStream . once ( "finish" ) ;
82
+ await readableSvgStream . pipe ( writableStream ) ;
83
+ } catch ( err ) {
84
84
readableSvgStream = null ; // Explicitly null because of v8 bug 6512.
85
85
writableStream . end ( ) ;
86
86
throw err ;
87
- } ) ;
87
+ }
88
88
}
89
89
90
- // Will be using promises to load document, pages and misc data instead of
90
+ // Will be using async/await to load document, pages and misc data instead of
91
91
// callback.
92
- const loadingTask = pdfjsLib . getDocument ( {
93
- data,
94
- cMapUrl : CMAP_URL ,
95
- cMapPacked : CMAP_PACKED ,
96
- fontExtraProperties : true ,
97
- } ) ;
98
- loadingTask . promise
99
- . then ( function ( doc ) {
100
- const numPages = doc . numPages ;
101
- console . log ( "# Document Loaded" ) ;
102
- console . log ( "Number of Pages: " + numPages ) ;
92
+ const loadingTask = async function ( ) {
93
+ const loadTask = pdfjsLib . getDocument ( {
94
+ data,
95
+ cMapUrl : CMAP_URL ,
96
+ cMapPacked : CMAP_PACKED ,
97
+ fontExtraProperties : true ,
98
+ } ) ;
99
+ const doc = await loadTask . promise ;
100
+ const numPages = await doc . numPages ;
101
+ console . log ( "# Document Loaded" ) ;
102
+ console . log ( "Number of Pages: " + numPages ) ;
103
+ console . log ( ) ;
104
+
105
+ let finalResult ;
106
+ const loadPage = async function ( pageNum ) {
107
+ const page = await doc . getPage ( pageNum ) ;
108
+ console . log ( "# Page " + pageNum ) ;
109
+ const viewport = page . getViewport ( { scale : 1.0 } ) ;
110
+ console . log ( "Size: " + viewport . width + "x" + viewport . height ) ;
103
111
console . log ( ) ;
104
112
105
- let lastPromise = Promise . resolve ( ) ; // will be used to chain promises
106
- const loadPage = function ( pageNum ) {
107
- return doc . getPage ( pageNum ) . then ( function ( page ) {
108
- console . log ( "# Page " + pageNum ) ;
109
- const viewport = page . getViewport ( { scale : 1.0 } ) ;
110
- console . log ( "Size: " + viewport . width + "x" + viewport . height ) ;
111
- console . log ( ) ;
112
-
113
- return page . getOperatorList ( ) . then ( function ( opList ) {
114
- const svgGfx = new pdfjsLib . SVGGraphics ( page . commonObjs , page . objs ) ;
115
- svgGfx . embedFonts = true ;
116
- return svgGfx . getSVG ( opList , viewport ) . then ( function ( svg ) {
117
- return writeSvgToFile ( svg , getFilePathForPage ( pageNum ) ) . then (
118
- function ( ) {
119
- console . log ( "Page: " + pageNum ) ;
120
- } ,
121
- function ( err ) {
122
- console . log ( "Error: " + err ) ;
123
- }
124
- ) ;
125
- } ) ;
126
- } ) ;
127
- } ) ;
128
- } ;
113
+ const opList = await page . getOperatorList ( ) ;
114
+ const svgGfx = new pdfjsLib . SVGGraphics ( page . commonObjs , page . objs ) ;
115
+ svgGfx . embedFonts = true ;
116
+ const svg = await svgGfx . getSVG ( opList , viewport ) ;
117
+ let result ;
129
118
130
- for ( let i = 1 ; i <= numPages ; i ++ ) {
131
- lastPromise = lastPromise . then ( loadPage . bind ( null , i ) ) ;
119
+ try {
120
+ result = await writeSvgToFile ( svg , getFilePathForPage ( pageNum ) ) ;
121
+ console . log ( "Page: " + pageNum ) ;
122
+ } catch ( err ) {
123
+ console . log ( "Error: " + err ) ;
132
124
}
133
- return lastPromise ;
134
- } )
135
- . then (
136
- function ( ) {
137
- console . log ( "# End of Document" ) ;
138
- } ,
139
- function ( err ) {
140
- console . error ( "Error: " + err ) ;
125
+ return result ;
126
+ } ;
127
+
128
+ try {
129
+ for ( let i = 1 ; i <= numPages ; i ++ ) {
130
+ finalResult = loadPage . bind ( null , i ) ;
141
131
}
142
- ) ;
132
+ console . log ( "# End of Document" ) ;
133
+ } catch ( err ) {
134
+ console . error ( "Error: " + err ) ;
135
+ }
136
+ return finalResult ;
137
+ } ;
138
+
139
+ loadingTask ( ) ;
0 commit comments