Thursday, 17 January 2019

PDF generate in SharePoint Online

You would try to use jspdf 1.2.60 as I found the author wrapped the jspdf in a anonymous function from v 1.3.0 above seems caused the error(ignore the html tag, as I tested in SPFx first).
Here is my sample test code in react script editor web part in modern page.

<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.60/jspdf.min.js"></script>
    <script type="text/javascript">        
        function SaveAsPDF() {
            var doc = new jsPDF();
            var specialElementHandlers = {
                '#editor': function (element, renderer) {
                    return true;
                }
            };
            doc.fromHTML($('div[class^="spFxJspdf"]').html(), 15, 15, {
                'width': 170,
                'elementHandlers': specialElementHandlers
            });
            doc.save('testfile.pdf');
        }
    </script>
    <div class="spFxJspdf_c9184c61">
        <div class="container_c9184c61">
            <div class="row_c9184c61">
                <div class="column_c9184c61">
                    <span class="title_c9184c61">Welcome to SharePoint!</span>
                    <p class="subTitle_c9184c61">Customize SharePoint experiences using Web Parts.</p>
                    <p class="description_c9184c61">SPFxJSPDF</p>
                    <a href="https://aka.ms/spfx" class="button_c9184c61">
                        <span class="label_c9184c61">Learn more</span>
                    </a>
                </div>
            </div>
        </div>
        <input type="button" id="btn_pdf" onclick="SaveAsPDF()" value="SavePDF">
    </div>

No comments:

Post a Comment