Skip to content

Commit 76edb33

Browse files
authored
Merge pull request #764 from jutunen/jspdf_master
XMP metadata inclusion not supported #763
2 parents 6b669d3 + 93a5337 commit 76edb33

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import './plugins/split_text_to_size';
1717
import './plugins/standard_fonts_metrics';
1818
import './plugins/svg';
1919
import './plugins/total_pages';
20+
import './plugins/xmp_metadata';
2021

2122
import './node_modules/cf-blob.js/Blob.js';
2223
import './node_modules/filesaver.js/FileSaver.js';

plugins/xmp_metadata.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/** ====================================================================
2+
* jsPDF XMP metadata plugin
3+
* Copyright (c) 2016 Jussi Utunen, [email protected]
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining
6+
* a copy of this software and associated documentation files (the
7+
* "Software"), to deal in the Software without restriction, including
8+
* without limitation the rights to use, copy, modify, merge, publish,
9+
* distribute, sublicense, and/or sell copies of the Software, and to
10+
* permit persons to whom the Software is furnished to do so, subject to
11+
* the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
* ====================================================================
24+
*/
25+
26+
/*global jsPDF */
27+
28+
/**
29+
* Adds XMP formatted metadata to PDF
30+
*
31+
* @param {String} metadata The actual metadata to be added. The metadata shall be stored as XMP simple value. Note that if the metadata string contains XML markup characters "<", ">" or "&", those characters should be written using XML entities.
32+
* @param {String} namespaceuri Sets the namespace URI for the metadata. Last character should be slash or hash.
33+
* @function
34+
* @returns {jsPDF}
35+
* @methodOf jsPDF#
36+
* @name addMetadata
37+
*/
38+
39+
(function (jsPDFAPI) {
40+
'use strict';
41+
var xmpmetadata = "";
42+
var xmpnamespaceuri = "";
43+
var metadata_object_number = "";
44+
45+
jsPDFAPI.addMetadata = function (metadata,namespaceuri) {
46+
xmpnamespaceuri = namespaceuri || "http://jspdf.default.namespaceuri/"; //The namespace URI for an XMP name shall not be empty
47+
xmpmetadata = metadata;
48+
this.internal.events.subscribe(
49+
'postPutResources',
50+
function () {
51+
if(!xmpmetadata)
52+
{
53+
metadata_object_number = "";
54+
}
55+
else
56+
{
57+
var xmpmeta_beginning = '<x:xmpmeta xmlns:x="adobe:ns:meta/">';
58+
var rdf_beginning = '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:jspdf="' + xmpnamespaceuri + '"><jspdf:metadata>';
59+
var rdf_ending = '</jspdf:metadata></rdf:Description></rdf:RDF>';
60+
var xmpmeta_ending = '</x:xmpmeta>';
61+
var utf8_xmpmeta_beginning = unescape(encodeURIComponent(xmpmeta_beginning));
62+
var utf8_rdf_beginning = unescape(encodeURIComponent(rdf_beginning));
63+
var utf8_metadata = unescape(encodeURIComponent(xmpmetadata));
64+
var utf8_rdf_ending = unescape(encodeURIComponent(rdf_ending));
65+
var utf8_xmpmeta_ending = unescape(encodeURIComponent(xmpmeta_ending));
66+
67+
var total_len = utf8_rdf_beginning.length + utf8_metadata.length + utf8_rdf_ending.length + utf8_xmpmeta_beginning.length + utf8_xmpmeta_ending.length;
68+
69+
metadata_object_number = this.internal.newObject();
70+
this.internal.write('<< /Type /Metadata /Subtype /XML /Length ' + total_len + ' >>');
71+
this.internal.write('stream');
72+
this.internal.write(utf8_xmpmeta_beginning + utf8_rdf_beginning + utf8_metadata + utf8_rdf_ending + utf8_xmpmeta_ending);
73+
this.internal.write('endstream');
74+
this.internal.write('endobj');
75+
}
76+
}
77+
);
78+
this.internal.events.subscribe(
79+
'putCatalog',
80+
function () {
81+
if (metadata_object_number) {
82+
this.internal.write('/Metadata ' + metadata_object_number + ' 0 R');
83+
}
84+
}
85+
);
86+
return this;
87+
};
88+
}(jsPDF.API));

0 commit comments

Comments
 (0)