pdfmake 0.3.0-beta.1 → 0.3.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Renderer.js CHANGED
@@ -78,6 +78,9 @@ class Renderer {
78
78
  case 'svg':
79
79
  this.renderSVG(item.item);
80
80
  break;
81
+ case 'attachment':
82
+ this.renderAttachment(item.item);
83
+ break;
81
84
  case 'beginClip':
82
85
  this.beginClip(item.item);
83
86
  break;
@@ -306,6 +309,27 @@ class Renderer {
306
309
  if (image.linkToDestination) {
307
310
  this.pdfDocument.goTo(image.x, image.y, image._width, image._height, image.linkToDestination);
308
311
  }
312
+ if (image.linkToFile) {
313
+ const attachment = this.pdfDocument.provideAttachment(image.linkToFile);
314
+ this.pdfDocument.fileAnnotation(
315
+ image.x,
316
+ image.y,
317
+ image._width,
318
+ image._height,
319
+ attachment,
320
+ // add empty rectangle as file annotation appearance with the same size as the rendered image
321
+ {
322
+ AP: {
323
+ N: {
324
+ Type: 'XObject',
325
+ Subtype: 'Form',
326
+ FormType: 1,
327
+ BBox: [image.x, image.y, image._width, image._height]
328
+ }
329
+ },
330
+ }
331
+ );
332
+ }
309
333
  }
310
334
 
311
335
  renderSVG(svg) {
@@ -326,6 +350,17 @@ class Renderer {
326
350
  SVGtoPDF(this.pdfDocument, svg.svg, svg.x, svg.y, options);
327
351
  }
328
352
 
353
+ renderAttachment(attachment) {
354
+ const file = this.pdfDocument.provideAttachment(attachment.attachment);
355
+
356
+ const options = {};
357
+ if (attachment.icon) {
358
+ options.Name = attachment.icon;
359
+ }
360
+
361
+ this.pdfDocument.fileAnnotation(attachment.x, attachment.y, attachment._width, attachment._height, file, options);
362
+ }
363
+
329
364
  beginClip(rect) {
330
365
  this.pdfDocument.save();
331
366
  this.pdfDocument.addContent(`${rect.x} ${rect.y} ${rect.width} ${rect.height} re`);
@@ -1,12 +1,15 @@
1
1
  import http from 'http';
2
2
  import https from 'https';
3
3
 
4
- const fetchUrl = url => {
4
+ const fetchUrl = (url, headers = {}) => {
5
5
  return new Promise((resolve, reject) => {
6
6
  const parsedUrl = new URL(url);
7
7
  const h = (parsedUrl.protocol === 'https:') ? https : http;
8
+ let options = {
9
+ headers: headers
10
+ };
8
11
 
9
- h.get(url, res => {
12
+ h.get(url, options, res => {
10
13
  if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { // redirect url
11
14
  fetchUrl(res.headers.location).then(buffer => {
12
15
  resolve(buffer);
@@ -34,11 +37,11 @@ class URLResolver {
34
37
  this.resolving = {};
35
38
  }
36
39
 
37
- resolve(url) {
40
+ resolve(url, headers = {}) {
38
41
  if (!this.resolving[url]) {
39
42
  this.resolving[url] = new Promise((resolve, reject) => {
40
43
  if (url.toLowerCase().indexOf('https://') === 0 || url.toLowerCase().indexOf('http://') === 0) {
41
- fetchUrl(url).then(buffer => {
44
+ fetchUrl(url, headers).then(buffer => {
42
45
  this.fs.writeFileSync(url, buffer);
43
46
  resolve();
44
47
  }, result => {
@@ -1,7 +1,10 @@
1
- const fetchUrl = url => {
1
+ const fetchUrl = (url, headers = {}) => {
2
2
  return new Promise((resolve, reject) => {
3
3
  const xhr = new XMLHttpRequest();
4
4
  xhr.open('GET', url, true);
5
+ for (let headerName in headers) {
6
+ xhr.setRequestHeader(headerName, headers[headerName]);
7
+ }
5
8
  xhr.responseType = 'arraybuffer';
6
9
 
7
10
  xhr.onreadystatechange = () => {
@@ -46,11 +49,11 @@ class URLBrowserResolver {
46
49
  this.resolving = {};
47
50
  }
48
51
 
49
- resolve(url) {
52
+ resolve(url, headers = {}) {
50
53
  if (!this.resolving[url]) {
51
54
  this.resolving[url] = new Promise((resolve, reject) => {
52
55
  if (url.toLowerCase().indexOf('https://') === 0 || url.toLowerCase().indexOf('http://') === 0) {
53
- fetchUrl(url).then(buffer => {
56
+ fetchUrl(url, headers).then(buffer => {
54
57
  this.fs.writeFileSync(url, buffer);
55
58
  resolve();
56
59
  }, result => {