Skip to content

Commit 5effa74

Browse files
committed
Fix image support with new save() method by passing PDF as a Uint8Array. Fixes parallax#55
1 parent 5303571 commit 5effa74

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

examples/basic.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<html>
33
<head>
44
<title>jsPDF</title>
5+
6+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
57
<link rel="stylesheet" type="text/css" href="css/smoothness/jquery-ui-1.8.17.custom.css">
68
<link rel="stylesheet" type="text/css" href="css/main.css">
79

@@ -11,6 +13,8 @@
1113
<script type="text/javascript" src="../libs/FileSaver.js/FileSaver.js"></script>
1214
<script type="text/javascript" src="../libs/BlobBuilder.js/BlobBuilder.js"></script>
1315

16+
<script type="text/javascript" src="../jspdf.plugin.addimage.js"></script>
17+
1418
<script type="text/javascript" src="../jspdf.plugin.standard_fonts_metrics.js"></script>
1519
<script type="text/javascript" src="../jspdf.plugin.split_text_to_size.js"></script>
1620
<script type="text/javascript" src="../jspdf.plugin.from_html.js"></script>
@@ -36,6 +40,8 @@
3640

3741
<h1>jsPDF Demos</h1>
3842

43+
<a href="javascript:demoImages()">Image</a>
44+
3945
<p>Examples for using jsPDF with Data URIs below. Go <a href="https://github.com/MrRio/jsPDF">back to project homepage</a>.</p>
4046

4147
<div id="tabs">

examples/js/basic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ function demoImages() {
253253
doc.addImage(imgData, 'JPEG', 10, 10, 50, 50);
254254
doc.addImage(imgData, 'JPEG', 70, 10, 100, 120);
255255

256-
doc.save('Test.pdf');
256+
doc.save('output.pdf');
257257

258258
}
259259

jspdf.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,10 +1295,6 @@ function jsPDF(/** String */ orientation, /** String */ unit, /** String */ form
12951295
return this
12961296
}
12971297

1298-
API.addImage = function(imageData, format, x, y, w, h) {
1299-
return this
1300-
}
1301-
13021298
/**
13031299
Sets font size for upcoming text elements.
13041300
@@ -1630,9 +1626,19 @@ function jsPDF(/** String */ orientation, /** String */ unit, /** String */ form
16301626
case undef:
16311627
return buildDocument();
16321628
case 'save':
1633-
//console.log(buildDocument());
16341629
var bb = new BlobBuilder;
1635-
bb.append(buildDocument());
1630+
var data = buildDocument();
1631+
1632+
// Need to add the file to BlobBuilder as a Uint8Array
1633+
var length = data.length;
1634+
var array = new Uint8Array(new ArrayBuffer(length));
1635+
1636+
for (var i = 0; i < length; i++) {
1637+
array[i] = data.charCodeAt(i);
1638+
}
1639+
1640+
bb.append(array);
1641+
16361642
var blob = bb.getBlob('application/pdf');
16371643
saveAs(blob, options);
16381644
break;

0 commit comments

Comments
 (0)