转:Generating PDFs from Web Pages on the Fly with jsPDF

The Portable Document Format has been one the major innovations in the fields of desktop publishing and office automations.

It’s widely used in web publishing too, but unfortunately very often in wrong ways – like using it to replace contents that should have been built with HTML. This causes many problems regarding usability, accessibility, SEO and so on.

However, there are some cases in which PDF files are required: when a document needs to be archived and must be used outside the web (for example an invoice) or when you need a deep control on printing.

It was just the need to control printing that brought me to research a way to easily generate a PDF.

The purpose of this article is not to simply explain how a PDF can be created (there are many easy way to do this), but also to focus on the circumstances where a PDF file can solve a problem, and how a simple tool like jsPDF can help with this.

Dealing with Printing

Anyone who has dealt with CSS printing rules knows how difficult it is to achieve a decent level of cross-browser compatibility (take a look, for example, at thePage-break support table at Can I Use). Therefore, when I need to build something that must be printed, I always try to avoid CSS, and the simplest solution is to use PDF.

 

I’m not talking here about the simple conversion of HTML to PDF. (I’ve tried several tools of that type, but none of them has fully satisfied me.) My goal is to have complete control over the positioning and size of elements, page breaks and so on.

In the past I’ve often used FPDF, a PHP tool that can easily give you such controls and that can be easily expanded with many plugins.

Unfortunately, the library seems to be abandoned (its last version dates back to 2011) (Update: actually, the latest version appears to be from December 2015), but thanks to some JavaScript libraries, we now have the ability to build PDF files directly in the clients (thus making their generation faster).

When I started my project, some months ago, I searched for a JS library, and finally I found two candidates: jsPDF and pdfmake. pdfmake seems to be well documented and very easy to use, but since it was a beta version, I decided for jsPDF.

PDF Building with jsPDF

The jsPDF documentation is fairly minimal, consisting of a single page along with some demos, and a little more information in the source file (or in its jsDoc pages), so keep in mind that using it for complex projects can be a little hard in the beginning.

Anyway, jsPDF is very easy for basic PDF files generation. Take a look to a simple “Hello World” example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Hello world</title>
</head>
<body>
    <h1>Hello world</h1>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.0.272/jspdf.debug.js"></script>
    <script type="text/javascript">
        var pdf = new jsPDF();
        pdf.text(30, 30, 'Hello world!');
        pdf.save('hello_world.pdf');
    </script>
</body>
</html>

This HTML page generates a one-page PDF file and saves it on your computer. First you have to link to the jsPDF library (in this case, from cdnjs.com), then a jsPDF instance is created, a line of text is added, and the result is saved as hello_world.pdf.

Note that I’ve used the 1.0.272 version, and that it’s not the latest: at the time of writing this, the most recent version is the 1.1.135, but it has many issues, so I am still using the previous one.

You can see how extremely simple it is to build a basic PDF file (you can find more examples at the jsPDF site).

Let’s try to build something harder.

The Flyer Project

The user interface allows the user to insert some basic data (a title, an abstract and a price). Optionally, an image can be added, otherwise a grey-boxed Special Offer title is displayed.

Other data (the agency name and its website URL and logo) are embedded in the application code.

The PDF can be previewed in a iframe (except Explorer or Edge) or directly downloaded.

When the Update preview or the Download buttons are clicked, the PDF is generated using jsPDF and passed to the iframe as a data URI string or saved to disk, as in the above example.

The PDF generation first creates a new instance a jsPDF object with these options: portrait orientation (p), millimeters units (mm), ‘A4’ format.

var pdf = new jsPDF('p', 'mm', 'a4');

Images are added using the addImage function. Note that every object placed in the PDF page must be exactly positioned. You have to take care of the coordinates of each object using the units declared.

// pdf.addImage(base64_source, image format, X, Y, width, height)
pdf.addImage(agency_logo.src, 'PNG', logo_sizes.centered_x, _y, logo_sizes.w, logo_sizes.h);

Images must be Base64 encoded: the agency logo is embedded in the script in this format, while the image loaded by the user is encoded using the readAsDataURL method in the$('#flyer-image').change listener.

The title is added using the textAlign function. Note that this function is not part of the jsPDF core, but, as suggested by the author in his examples, the library can be easily expanded using its API. You can find the textAlign() function at the top of flyer builder script:

pdf.textAlign(flyer_title, {align: "center"}, 0, _y);

This function calculates the X coordinate of the the text string to make it centered, and then calls the native text() method:

pdf.text(text string, X, Y);

To change text properties, you can use the setFontSize()setFont()setTextColor() andsetFontType() methods.

To set a 20pt Times Bold red string, for example, you need to type this:

pdf.setFontSize(20);
pdf.setFont("times");
pdf.setFontType("bold");
pdf.setTextColor(255, 0, 0);
pdf.text(10,10, 'This is a 20pt Times Bold red string');

The Special offer grey box and the price circle use two similar methods: roundedRect() andcircle(). Both of them require top-left coordinates, size values (the width and height in the first case and the radius in the second one):

pdf.roundedRect( X, Y, width, height, radius along X axis, radius along Y axis, style);
pdf.circle( X, Y, radius, style);

The style parameters refers to the fill and stroke properties of the object. Valid styles are: S [default] for stroke, F for fill, and DF (or FD) for fill and stroke.

Fill and stroke properties must be set in advance using setFillColor and setDrawColor, which require a RGB value and setLineWidth that requires the line width value in the unit declared at inception of PDF document.

The complete code is available in the CodePen demo:

HTML

<div class="container-fluid flyer-builder">
        <h1>Flyer Builder</h1>
        <p><em>Forked from <a target="_blank" href="http://codepen.io/massimo-cassandro/pen/qOrJNx">this original Pen by Massimo Cassandro</a>, for his SitePoint article on <a target="_blank"  href="http://sitepoint.com/generating-pdfs-from-web-pages-on-the-fly-with-jspdf">Generating PDFs from Web Pages on the Fly with jsPDF</a>. The preview doesn't work  with Chrome or Safari in this demo since it has been loaded in a iframe. Use Firefox or try at <a href="http://www.primominuto.net/articoli/pdf_flyer/flyer.html">www.primominuto.net/articoli/pdf_flyer/flyer.html</a></em></p>
    
        <div class="row">
                <div class="col-sm-6 loc_form">

                        <div class="form-group">
                                <label for="flyer-image" class="control-label">Image</label>
                                <input id="flyer-image" type="file" tabindex="1">
                        </div>

                        <div class="row">
                                <div class="col-sm-7">
                                        <div class="form-group">
                                                <label for="flyer-title" class="control-label">Title</label>
                                                <input required class="form-control" id="flyer-title" placeholder="Main title" value="Really incredible!!" maxlength="255" type="text" tabindex="2">
                                        </div>
                                </div>
                                <div class="col-sm-3">
                                        <div class="form-group">
                                                <label for="flyer-title-size" class="control-label">Size (pt)</label>
                                                <input required class="form-control" id="flyer-title-size" value="60" min="1" step="1" type="number" tabindex="3" title="Title size: tune it to fit the available space">
                                        </div>
                                </div>
                                <div class="col-sm-2">
                                        <div class="form-group">
                                                <label for="flyer-title-color" class="control-label">Color</label>
                                                <input required class="form-control" id="flyer-title-color" value="#0080FF" type="color" tabindex="4" title="Title color">
                                        </div>
                                </div>
                        </div>

                        <div class="form-group">
                                <label class="control-label" for="flyer-description">Description</label>
                                <textarea class="form-control" id="flyer-description" placeholder="Insert a short description taking care of the available space">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nemo, nihil officia neque ad expedita consequatur quae! Voluptate, incidunt, earum, sit, eveniet harum ratione expedita quibusdam possimus sed laboriosam dolore ut recusandae eos. Ipsa,natus pariatur iste dolorum optio nostrum consectetur!</textarea>
                        </div>

                        <div class="row">
                                <div class="col-sm-4">
                                        <div class="form-group">
                                                <label for="flyer-price" class="control-label">Price</label>
                                                <input required class="form-control" id="flyer-price" placeholder="Enter price (w/o decimals)" value="100" type="number" tabindex="5" step="any" min="0">
                                        </div>
                                </div>
                                <div class="col-sm-3">
                                        <div class="form-group">
                                                <label for="flyer-price-currency" class="control-label">Currency</label>
                                                <select required class="form-control" id="flyer-price-currency" tabindex="6">
                                                        <option value="&euro;">&euro;</option>
                                                        <option value="$">$</option>
                                                        <option value="&pound;">&pound;</option>
                                                </select>
                                        </div>
                                </div>
                                <div class="col-sm-2">
                                        <div class="form-group">
                                                <label for="flyer-price-color" class="control-label">Color</label>
                                                <input required class="form-control" id="flyer-price-color" value="#cc0000" type="color" tabindex="8" title="Price color">
                                        </div>
                                </div>
                        </div>
                        <hr>
                        <div class="row">
                                <div class="col-sm-6">
                                        <div class="form-group">
                                                <button id="flyer_preview_btn" type="button" class="btn btn-primary btn-block" tabindex="9">Update preview</button>
                                        </div>
                                </div>
                                <div class="col-sm-6">
                                        <div class="form-group text-right">
                                                <button id="flyer_download_btn" type="button" class="btn btn-default btn-xs btn-block" tabindex="10">Download</button>
                                        </div>
                                </div>
                        </div>

                </div>

                <div class="col-sm-6">
                        <iframe id="pdf_preview" type="application/pdf" src=""></iframe>
                </div>

        </div>
</div>

css

.flyer-builder {
    position: relative;
    height: 100%;


    & iframe,
    .no_iframe {
        width:100%;
        height:400px;
        border:1px solid #666;
        background-color: #ddd;
    }

    & .no_iframe {
        & > div {
            //margin:0 .3em;
            width:80%;
            position: absolute;
            top:50%;
            left:50%;
            transform: translate(-50%, -50%);
            font-weight: normal;
        }
    }

    @media all and (min-width:768px) {
        & .btn.btn-block {
            display: inline;
            width: auto;
        }
    }
}

js

(function() {
        "use strict";

        /*
        Refs:
        http://mrrio.github.io/jsPDF/
        https://github.com/MrRio/jsPDF
        https://mrrio.github.io/jsPDF/doc/symbols/jsPDF.html
    */

        // some variables
        var agency_logo = {
                        src: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyAAAAEdCAMAAADD1fMAAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAABhQTFRFAHC6RpjOtdbr1uj0f7jd7/b7////IYLCvV4sVgAAIv1JREFUeNrsnYvao6oOQE0I5v3feFqvqIQEqv61k5z9nT17tIrAIhcCdL1LiwTGR5UXAb3RWqTzKmgS6rqOwkMKG4m7DrzRHJD75NXj3og8YViOMBY2eqs5ILdZLN0kjF/e7xDmorqN5YDcJkuveyEC32tpBeqSgnqzOSB3WS3dRpi+UY1EhG0xgzecA3Kfi76VrwsS4bGM5A3ngNxuYSX973sYCcSZArqN5YDc1P+6vPBX6JE8He6mOyB/q0AWRuJ30tH5VIgDcpMU+uDoj/yNOxwzfseWXp8KcUDu8H87Vfh2hyQQ6MVCJ8QB+VMLa6NIbktFCQhsK5I3ngNyk4tu6pEMl2sSIxzMPhXigNwjNFkraNYkF/kkL5/DWgScSu1TIQ7ITS56jErI6KBKwslsWN89xHcj+FSIA3Kjiz4vB7GqkXEgPwGTiHVszBMg4FMhDsitLvqcgBUJ6nrrS5u0cBLeZFSiwVPg91VSd9MdkFskJl109Upqh/RRn7xJwTIrr+tE1WAkM+c4FnRJr/RArwNyg4W1jMZDKBe6aHdHBK1ylE8eyCshNACCnm7igNyiQGAzQOMw3TEaLtW21kXCA7RjQScdkq7w8jZ0QD6UwMDSP7BNjoVtInlE/nM+Fi9jKMug5SCXoJ8Tt78ckEo3oyBvKtalSGFwJWL/11pkYHUoTBy0Brz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A/LwGYYGQZKmgW1cOyH8qkbP6IaxLBcEdEAfkP5YwehrLUvL3VqRJlHZIuuJs6m5YwrnscVwH5McJyW/Vg+Nyj8lfT2ZGIh7mBN2Nd0B+VBBycGyXCmI6kT6vJ5w395lhcTvMAflNRySsnVzcu3rECDAOrOy37h22BnJCHBDXNFI645Dz6NsnOiD/tb8CpT0Sgy/4cED+b3nPmoC01U9Et7EckP9cf7xDVXO679aNH3aW8xVRDsh/7IKs+bzLrnHT+VQ0TZV4JTkg/zEfGwD2O7rb91d0cUB+0v3gEA9ex3D651uNRJ9Jd0D+c/0hI+BwOCD/u39eu5zQxQH5nwTYpwAdEBfRhvIJDgfEpeChsxtYDoiLbGH5DKAD4uKAOCAujSaW14ED4iIKdr6k1gFxKdhYyUoPD/g6IC47CbwS4hFfB8RlLxHmPRuCL4pyQFwyfgh0QAHJc04cEJe8FnnvYOIG1rfJPwEGAAoLBPDggh+JAAAAAElFTkSuQmCC',
                        w: 800,
                        h: 285
                },
                agency_name = 'Travel & Holidays',
                agency_site_url = 'www.travelandholidays.com',
                footer = agency_name + ' - ' + agency_site_url,

                page_size = 'a4',
                page_width = 210, // mm
                page_margin = 10, // mm
                content_width = page_width - (page_margin * 2), // available width for the content

                _y, _x, // drawing coord
                color_array, _string, lineHeight, y_correction, // some variables

                vspace = 10 // standard vertical space between elements

        ;

        // some variables
        var can_display_preview = true, // if true a preview of the PDF can be displayed in the iframe,
                // this value is set to false if the browser can't display the preview
                preview_container = $('#pdf_preview'),
                update_preview_button = $('#flyer_preview_btn'),
                download_button = $('#flyer_download_btn');

        // preview can be displayed?
        if (navigator.msSaveBlob) { // older IE
                update_preview_button.prop('disabled', true);
                can_display_preview = false;
                preview_container.replaceWith(
                        '<div class="no_iframe">' +
                        '<div>' +
                        "The preview can't be displayed" +
                        '</div>' +
                        '</div>'
                );
        }

        // utilities
        var hex2rgb = function(hex_string) {
                        if (/^#/.test(hex_string)) {
                                hex_string = hex_string.substr(1);
                        }
                        if (hex_string.length === 3) {
                                hex_string = hex_string.replace(/\w/, function(match) {
                                        return String(match) + String(match);
                                });
                        }

                        return {
                                red: parseInt(hex_string.substr(0, 2), 16),
                                green: parseInt(hex_string.substr(2, 2), 16),
                                blue: parseInt(hex_string.substr(4, 2), 16)
                        };
                },

                px2mm = function(pixel) {
                        // px to inches
                        var inches = pixel / 72;
                        return inches * 25.4;
                },

                mm2px = function(millimeters) {
                        // mm to inches
                        var inches = millimeters / 25.4;
                        return inches * 72;
                },

                // function to calculate and check img sizes
                imgSizes = function(img_w, img_h, img_mm_w) {
                        /*
            img_w and img_h represent the original image size, in pixel
            img_mm_w is the desidered rendered image size, in millimeters

        */

                        if (img_mm_w > content_width) { // this should be never used...
                                img_mm_w = content_width;
                        }

                        if (mm2px(img_mm_w) > img_w) {
                                throw 'The `img_mm_w` parameter is too big';
                        }

                        var img_mm_h = Math.round((px2mm(img_h) * img_mm_w) / px2mm(img_w));

                        return {
                                w: img_mm_w,
                                h: img_mm_h,
                                centered_x: (page_width - img_mm_w) / 2
                        };
                };

        try {

                // image reading
                // More info at https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
                // for more examples about file api
                // take a look at https://scotch.io/tutorials/use-the-html5-file-api-to-work-with-files-locally-in-the-browser

                var flyer_img = $('#flyer-image'),
                        img_data = null;

                $('#flyer-image').change(function() {

                        // temporary disabling buttons while parsing image
                        update_preview_button.prop('disabled', true);
                        download_button.prop('disabled', true);

                        /*
                getting the file

                flyer_img[0] : transforms the jQuery reference to a DOM object
                files[0]     : refers to the file the the user has chosen

                use `console.log(user_file);` to show some info about the file

            */

                        var user_file = flyer_img[0].files[0];

                        img_data = {
                                type: user_file.type === 'image/jpeg' ? 'JPEG' : 'PNG' // maybe you should add some controls to prevent loading of other file types
                        };

                        var reader = new FileReader();
                        reader.onload = function(event) {
                                img_data.src = event.target.result;

                                // we need this to get img dimensions
                                var user_img = new Image();
                                user_img.onload = function() {
                                        img_data.w = user_img.width;
                                        img_data.h = user_img.height;

                                        // restoring buttons
                                        download_button.prop('disabled', false);
                                        if (can_display_preview) {
                                                update_preview_button.prop('disabled', false);
                                        }
                                };
                                user_img.src = img_data.src;
                        };

                        //when the file is read it triggers the onload event above.
                        reader.readAsDataURL(user_file);
                });

                //!pdf builder
                var createPDF = function(update_preview) {
                        /*
                update_preview:
                    ==> true: shows pdf online
                    ==> false: downloads the pdf
            */

                        _y = page_margin; // vertical starting point

                        // form data
                        var flyer_title = $('#flyer-title').val(),
                                flyer_title_size = $('#flyer-title-size').val(),
                                flyer_title_color = $('#flyer-title-color').val(),
                                flyer_description = $('#flyer-description').val(),
                                flyer_price = $('#flyer-price').val(),
                                flyer_price_currency = $('#flyer-price-currency').val(),
                                flyer_price_color = $('#flyer-price-color').val();

                        var pdf = new jsPDF('p', 'mm', page_size),
                                text_baseline,

                                // some colors:
                                light_grey = 237,
                                grey = 128,
                                black = 0,
                                white = 255;

                        // Optional - set properties of the document
                        pdf.setProperties({
                                title: flyer_title,
                                subject: footer,
                                author: 'me',
                                creator: 'Flyer Builder & jsPDF'
                        });

                        // !logo
                        var logo_sizes = imgSizes(agency_logo.w, agency_logo.h, 60);
                        pdf.addImage(agency_logo.src, 'PNG', logo_sizes.centered_x, _y, logo_sizes.w, logo_sizes.h);

                        // fonts initializing
                        pdf.setFont("helvetica");
                        pdf.setFontType("bold");

                        // !main title
                        color_array = hex2rgb(flyer_title_color);
                        pdf.setTextColor(color_array.red, color_array.green, color_array.blue);

                        pdf.setFontSize(flyer_title_size);

                        lineHeight = px2mm(pdf.getLineHeight(flyer_title));

                        _y += (logo_sizes.h + vspace + lineHeight);

                        pdf.textAlign(flyer_title, {
                                align: "center"
                        }, 0, _y);

                        _y += vspace;

                        // !user image
                        if (img_data) {
                                var img_sizes = imgSizes(img_data.w, img_data.h, content_width);
                                pdf.addImage(img_data.src, img_data.type, img_sizes.centered_x, _y, img_sizes.w, img_sizes.h);
                                _y += img_sizes.h;

                        } else {
                                // if we haven't an image, a grey box with a text is added

                                var box_height = 80;

                                pdf.setFillColor(light_grey);
                                pdf.roundedRect(page_margin, _y, content_width, box_height, 5, 5, 'F');
                                pdf.setFontSize(60);
                                pdf.setTextColor(white);
                                _string = 'SPECIAL OFFER';
                                lineHeight = px2mm(pdf.getLineHeight(_string));

                                // y_correction: value to be added to y coord of the grey box to have text vertically centered
                                // it is empirically calculated adding 1/3 of text line height to half box height
                                y_correction = box_height / 2 + lineHeight / 3;

                                pdf.textAlign(_string, {
                                        align: "center"
                                }, 0, _y + y_correction);

                                _y += box_height;
                        }

                        // !price
                        // first: creating a circle that overlaps the bottom side of the image
                        var circle_radius = 30;
                        color_array = hex2rgb(flyer_price_color);
                        pdf.setFillColor(color_array.red, color_array.green, color_array.blue);

                        // _x and _y refer to center of the circle
                        _x = content_width - circle_radius; // circle ends at `page_margin` millimeters from the image right side

                        pdf.circle(_x, _y, circle_radius, 'F'); // circle overlaps image for 1/2 of its height

                        pdf.setFontSize(60);
                        pdf.setFont("times");
                        pdf.setFontType("bold");

                        _string = flyer_price_currency + parseInt(flyer_price); // decimals are removed

                        lineHeight = px2mm(pdf.getLineHeight(_string));
                        y_correction = lineHeight / 3;

                        pdf.setTextColor(white);
                        pdf.textAlign(_string, {
                                align: "centerAtX"
                        }, _x, _y + y_correction);

                        // !description
                        if (flyer_description) {
                                pdf.setFontSize(20);
                                pdf.setFont("helvetica");
                                pdf.setFontType("italic");
                                pdf.setTextColor(grey);

                                var lineWidth = content_width - (circle_radius * 2) - (page_margin * 2);
                                _y += page_margin;

                                var line_height = 12; // mm

                                var description_lines = pdf.splitTextToSize(flyer_description, lineWidth);
                                //pdf.text(page_margin, _y, description_lines); // doesn't allows to change line spacing

                                for (var i = 0; i < description_lines.length; i++) {
                                        pdf.text(page_margin, _y, description_lines[i]);
                                        _y += line_height;
                                }

                        }

                        // !footer
                        _y = 287;
                        pdf.setFontSize(9);
                        pdf.setFontType("normal");
                        pdf.setTextColor(black);
                        pdf.textAlign(footer, {
                                align: "center"
                        }, 0, _y);

                        // ****************************
                        // output
                        if (update_preview) {
                                preview_container.attr('src', pdf.output('datauristring'));
                        } else {
                                pdf.save('flyer ' + flyer_title + '.pdf');
                        }

                }; // end createPDF

                // !buttons
                update_preview_button.click(function() {
                        createPDF(true);
                });

                $('#flyer_download_btn').click(function() {
                        createPDF(false);
                });

        } catch (e) {
                console.log(e);
        }

})();

Conclusion

This basic example shows how it’s possible to build a very basic flyer with jsPDF.

Its use could be easy, but the lack of a complete documentation makes every step really complicated.

I’m still looking around for other solutions, keeping my eye on others like pdfmake. But ultimately, I think the only really definitive solution can be better browser support for printing CSS rules!

Have you used jsPDF or something similar? What was your experience? Let me know in the comments.

posted on 2016-02-28 22:56  加油_linda  阅读(4681)  评论(0编辑  收藏  举报