Skip to content

Commit

Permalink
Merge pull request #16 from SarahTrees/patch-1
Browse files Browse the repository at this point in the history
Add callback function after Download
  • Loading branch information
sharonchoong authored Jun 23, 2024
2 parents 9f64825 + b595ff0 commit 9fc9a4a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ See `index.html` for an example of how to use.
- **excludeByCSSSelector** (string): _e.g. `[stroke='red'], [stroke='green'], [display='none'], .text-muted`. Elements matching the specified [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors) will not be included in the generated file. This can be used to remove unwanted/unsupported elements of the SVG from the exported file, or to optimize performance for large SVGs. Please read **Optimizing for large SVGs** and **Not Supported** below for more detail._
- **transparentBackgroundReplace** (string): _the color to be used to replace a transparent background in JPEG format export. Default is `white`_
- **allowCrossOriginImages** (bool): _If the SVG contains images, this option permits the use of images from foreign origins. Defaults to `false`. Please read **Images within SVG** below for more detail._
- **onDone** (function): _an optional callback function that will be called after the file export has completed._
- **pdfOptions**
- **pageLayout** (object): _e.g. `{ margin: 50, layout: "landscape" }`. This is provided to PDFKit's `addPage`. When the options **width** and **height** are not specified, a minimum size of 300x300 is used for the PDF page size; otherwise the page size wraps around the SVG size. Please see the [PDFKit documentation](https://pdfkit.org/docs/getting_started.html#adding_pages) for more info_
- **addTitleToPage** (bool): _Default is `true`_
Expand Down
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ <h3>Demo</h3>
<button id="btn_export_pdf">PDF (with captions)</button>
<button id="btn_export_svg_filters">SVG (filtered)</button>
</div>
<p>Note: for the "PNG (svg string)" export, the red circle does not render as red because the serialized string does not contain the "color: red" style for "currentColor", which is actually found on the SVG's container</div>
<p>Note: for the "SVG (filtered)" export, circles and the "Subtitle" text with "font-size: 10" are filtered out</div>
<p>Note: for the "PNG (svg string)" export, the red circle does not render as red because the serialized string does not contain the "color: red" style for "currentColor", which is actually found on the SVG's container</p>
<p>Note: for the "SVG (filtered)" export, circles and the "Subtitle" text with "font-size: 10" are filtered out</p>
</body>
<script>
document.querySelector("#btn_export_svg").onclick = function(){
Expand Down
13 changes: 10 additions & 3 deletions svg-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@
pdfTextFontFamily: "Helvetica",
pdfTitleFontSize: 20,
pdfCaptionFontSize: 14
}
},
onDone: null
};

//original size
Expand Down Expand Up @@ -145,6 +146,9 @@
if (options && options.excludeByCSSSelector && typeof(options.excludeByCSSSelector) === "string") {
_options.elementsToExclude = svgElement.querySelectorAll(options.excludeByCSSSelector);
}
if (options && options.onDone && typeof(options.onDone) === "function") {
_options.onDone = options.onDone;
}

setPdfOptions(options);
}
Expand Down Expand Up @@ -274,7 +278,7 @@
} else {
blob = new Blob([new Uint8Array(array)], { type: mimeString });
}
return navigator.msSaveBlob(blob, name);
navigator.msSaveBlob(blob, name);
} else {
var link = document.createElement("a");
link.download = name;
Expand All @@ -283,6 +287,9 @@
link.click();
document.body.removeChild(link);
}
if (_options.onDone) {
_options.onDone();
}
}

function downloadSvg(svg, svgName, options) {
Expand Down Expand Up @@ -470,4 +477,4 @@
exports.downloadPdf = downloadPdf;
Object.defineProperty(exports, "__esModule", { value: true });
})
));
));
Loading

0 comments on commit 9fc9a4a

Please sign in to comment.