Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export to pdf on website #496

Closed
thorst opened this issue Mar 23, 2017 · 4 comments
Closed

Export to pdf on website #496

thorst opened this issue Mar 23, 2017 · 4 comments

Comments

@thorst
Copy link

thorst commented Mar 23, 2017

I'm trying to take the diagram and put it in a pdf. Any direction would be helpful.

I think it may be easier to generate a png or jpg on the website in mermaid, is that possible? I've tried to add the svg to the pdf and haven't been successful. I've also tried to take the long route and convert the svg to canvas and then to png and that hasn't gone well.

Ive been playing with pdfKit and jsPdf. I have the basic generation of the pdf fine it just that I cant get the mermaid diagram on the pdf.

@sooraj771989
Copy link

Did You Get the solution for this ? Please help me out

@thorst
Copy link
Author

thorst commented Apr 18, 2017 via email

@tylerlong
Copy link
Collaborator

There is a manual way: display mermaid charts in Chrome browser, then print the page, select destination as "save as PDF".

image

@thorst
Copy link
Author

thorst commented Apr 21, 2017

Using https://github.com/alafr/SVG-to-PDFKit and https://mozilla.github.io/pdf.js/

Semi-pseudo code

    <script src="~/Scripts/lib/SVG-to-PDFKit/pdfkit.js"></script>
    <script src="~/Scripts/lib/SVG-to-PDFKit/blobstream.js"></script>
    <script src="~/Scripts/lib/SVG-to-PDFKit/source.js"></script>

blobToBase64= function (blob, callback) {
        // Borrowed
        var reader = new FileReader();
        reader.onload = function () {
            var dataUrl = reader.result;
            var base64 = dataUrl.split(',')[1];
            callback(base64);
        };
        reader.readAsDataURL(blob);
    };
    Base64toArrayBuffer= function (base64_string) {
        // Borrowed
        var raw = window.atob(base64_string);
        var rawLength = raw.length;
        var array = new Uint8Array(new ArrayBuffer(rawLength));

        for (var i = 0; i < rawLength; i++) {
            array[i] = raw.charCodeAt(i);
        }
        return array;
    };

let doc = new PDFDocument({ compress: true });

let hiddenDiv = $("#hidden-div").html('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">' + app.mermaid.html + '</svg>');
            SVGtoPDF(doc, hiddenDiv[0].firstChild, doc.x, doc.y, { useCSS: true });
            doc.y = doc.y + (parseFloat($("#mermaidId0").attr("height")) * 72 / 96) // Best solution provided by developer

// Works on blob
        let stream = doc.pipe(blobStream());
        // Events are: "finish", "Data", "end"
        stream.on('finish', function () {

            // Convertintg blob to base64
            let blob = stream.toBlob('application/pdf');
            app.documentation.blobToBase64(blob, function (d) {

                // Put in global
                app.documentation.base64 = d;

                // Display in viewer
                var raw = app.documentation.Base64toArrayBuffer(app.documentation.base64);
                PDFViewerApplication.open(raw);

                // Overwrite filename for saving
                PDFViewerApplication.url = app.interface.current.name + ".pdf";
            });
        });

        // Close pdf document
        doc.end();

app.mermaid.html is a variable that contains the html of the mermaid. Basically i get the base64 pdf and save that off to a database for a revision history, I also render it using pdf.js (PDFViewerApplication).

mgenereu pushed a commit to mgenereu/mermaid that referenced this issue Jun 25, 2022
Bumps [cssnano](https://github.com/cssnano/cssnano) from 5.0.9 to 5.0.10.
- [Release notes](https://github.com/cssnano/cssnano/releases)
- [Commits](https://github.com/cssnano/cssnano/compare/cssnano@5.0.9...cssnano@5.0.10)

---
updated-dependencies:
- dependency-name: cssnano
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants