buli
2022-05-13 2f4492ee18f90274582fcc2bb06f5e9bf64136e8
force-app/main/default/pages/QuotePDF.page
@@ -1,5 +1,4 @@
<apex:page Controller="QuotePDFController" showHeader="false" sidebar="false" id="allPage" action="{!init}" >
    <!-- <apex:page Controller="QuotePDFController" showHeader="false" sidebar="false" id="allPage" action="{!init}" renderAs="pdf"> -->
<apex:page Controller="QuotePDFController" showHeader="false" sidebar="false" id="allPage" action="{!init}" renderAs="pdf">
<head>
    <style>
        @page {
@@ -9,8 +8,6 @@
    </style>
</head>
<body style="font-family: Arial Unicode MS; page-break-inside: auto">
    <div id="pdf-wrapper">
        <div class="pdf-page">
            <table border="0" width="100%" style="border-collapse: collapse;">
                <tr>
                    <td style="text-align:right;">{!quoteInfo.quoteDate}</td>
@@ -25,7 +22,7 @@
                    <td style="text-align:left;">{!$Label.Label_From}:{!quoteInfo.dealerName}</td>
                </tr>
                <tr>
                    <td style="text-align:left;">{!$ObjectType.Contact.label}:<span id="dealerContactName">{!quoteInfo.dealerContactName}</span>&nbsp;&nbsp;&nbsp;&nbsp;{!$ObjectType.Contact.fields.MobilePhone.label}:<span>{!quoteInfo.dealerContactPhone}</span></td>
            <td style="text-align:left;">{!$ObjectType.Contact.label}:{!quoteInfo.dealerContactName}&nbsp;&nbsp;&nbsp;&nbsp;{!$ObjectType.Contact.fields.MobilePhone.label}:{!quoteInfo.dealerContactPhone}</td>
                </tr>
                <tr>
                    <td style="text-align:left;">{!$ObjectType.Account.fields.Address1__c.label}:{!quoteInfo.dealerAddress}</td>
@@ -43,10 +40,10 @@
                    <td style="text-align:left;">{!$Label.Label_To}:{!quoteInfo.accountName}</td>
                </tr>
                <tr>
                    <td style="text-align:left;" id="accountContactName">{!quoteInfo.accountContactName}</td>
            <td style="text-align:left;">{!quoteInfo.accountContactName}</td>
                </tr>
                <tr>
                    <td style="text-align:left;">{!$ObjectType.Contact.fields.Phone.label}:<span id="accountContactPhone">{!quoteInfo.accountContactPhone}</span>&nbsp;&nbsp;&nbsp;&nbsp;{!$ObjectType.Contact.fields.Email.label}:<span id="accountContactEmail">{!quoteInfo.accountContactEmail}</span></td>
            <td style="text-align:left;">{!$ObjectType.Contact.fields.Phone.label}:{!quoteInfo.accountContactPhone}&nbsp;&nbsp;&nbsp;&nbsp;{!$ObjectType.Contact.fields.Email.label}:{!quoteInfo.accountContactEmail}</td>
                </tr>
            </table>
        
@@ -100,7 +97,6 @@
            6)  Configuration as per attached.<br/>
            7)  Delivery lead time is {!quoteInfo.note7} days after receipt of payment.<br/> -->
            
        </div>
        <apex:outputPanel rendered="{!pageCnt > 1}" layout="none">
            <div style="page-break-after: always;"/>
        </apex:outputPanel>
@@ -109,7 +105,6 @@
        <apex:repeat value="{!printRecords}" var="set">
            <!-- <apex:variable value="{!1}" var="cnt2" /> -->
            <apex:repeat value="{!set.setPage}" var="sp">
                <div class="pdf-page">
                    <table border="1" width="100%" style="border-collapse: collapse;table-layout: fixed; font-size: 12px;">
                        <colgroup>
                            <col width="3%" />
@@ -155,8 +150,6 @@
                        </tr>
                        </apex:repeat>
                    </table>
                </div>
    
            <apex:outputPanel rendered="{!cnt1 < pageCnt - 1}" layout="none">
                <div style="page-break-after: always;"/>
@@ -166,191 +159,5 @@
            <apex:variable value="{!cnt1 + 1}" var="cnt1" />
            </apex:repeat>
        </apex:repeat>
    </div>
</body>
<style>
    /* 20220221 PI改造 by 徐亮 start */
    body{margin: 0 auto;width: 780px;}
    .title1{height: 30px;}
    .title2{height: 110px;}
    /* 20220221 PI改造 by 徐亮 end */
</style>
<!-- 20220221 PI改造 by 徐亮 start  -->
<apex:includeScript value="{! URLFOR($Resource.AWSService, 'AWSService.js') }" />
<script src="../../soap/ajax/53.0/connection.js" type="text/javascript"></script>
<apex:stylesheet value="{!URLFOR($Resource.blockUIcss)}"/>
<apex:includeScript value="{!URLFOR($Resource.jquery183minjs)}"/>
<apex:includeScript value="{!URLFOR($Resource.PleaseWaitDialog)}"/>
<apex:includeScript value="{!URLFOR($Resource.jspdf)}"/>
<apex:includeScript value="{!URLFOR($Resource.html2canvas)}"/>
<!-- 20220221 PI改造 by 徐亮 end  -->
<!-- 20220221 PI改造 by 徐亮 start  -->
<script>
    AWSService.sfSessionId = '{!GETSESSIONID()}';
    sforce.connection.serverUrl = '{!$Site.Prefix}/services/Soap/u/53.0';
    var staticResourceContact = JSON.parse('{!staticResourceContact}');
    var staticResourceOpportunity = JSON.parse('{!staticResourceOpportunity}');
    var staticResourceFile = JSON.parse('{!staticResourceFile}');
    function Fun(pdf) {
        var form = jQuery("<form method='post'></form>");
        jQuery(document.body).append(form);
        let href = window.location.href
        let arr = href.split('/')
        form.attr({ "action": staticResourceFile.hostUrl + staticResourceFile.extraInfo + "?from=" + encodeURIComponent(href) + "&fileName=" + encodeURIComponent(arr[arr.length - 1].split('?')[0]) });
        // form.attr({"action":"http://127.0.0.1:8080/a/d?from="+encodeURIComponent(window.location.href)});
        let input = jQuery("<input type='hidden'/>");
        input.attr({ "name": "base64Str" });
        input.val(pdf.output('datauristring').substr(28));
        form.append(input);
        form.submit();
        // jQuery("body > *").each(function(){
        //    jQuery(this).css('display','none');
        // })
        // jQuery("body").append('<embed style="position:absolute; left: 0; top: 0;" width="100%" height="100%" src="'+pdf.output('datauristring')+'" type="application/pdf"/>')
    }
    let id = "pdf-wrapper";
    var target = document.getElementById(id);
    // 把imageDta转换成url
        // page_height:一页的高度
        function ImageDataToUrl(img_data,page_height){
            let cvs = document.createElement("CANVAS");
            cvs.width = img_data.width;
            cvs.height = page_height;
            var ctx= cvs.getContext("2d");
            ctx.fillStyle="white";
            ctx.fillRect(0,0,cvs.width,cvs.height);// 填充canvas所有区域为白色
            ctx.putImageData(img_data,0,0);
            return cvs.toDataURL('image/jpeg', 1.0);
        }
        function jsPdfDownload() {
            let jtargets = jQuery("#pdf-wrapper .pdf-page");
            let j_arr = new Array(jtargets.length + 1).join(0).split('').map(function () { return false });
            let landscape = false;
            let pw = 595.28;
            let ph = 841.89;
            const marginw = 40;
            const marginh = 40;
            let orientation = '';
            if (landscape) {
                pw += ph; ph = pw - ph; pw = pw - ph;
                orientation = 'l';
            }
            var pdf = new jsPDF(orientation, 'pt', 'a4');// 第一个参数填字母l,注意不是数字1,为横向pdf
            jtargets.each(function (i, e) {
                e.style.background = "#FFFFFF";
            })
            let rei = 0;
            let recursion = null;
            recursion = function (i) {
                if (i >= jtargets.length) {
                    Fun(pdf);
                    return;
                }
                e = jtargets[i];
                html2canvas(e, {
                    scale: 2,
                    onrendered: function (canvas) {
                        /**
                        本方法需要做几个事情
                        1.需要把canvas的内容分页显示到pdf的页中
                        2.canvas的宽高需要和pdf的宽高进行映射
                        3.截取到的内容需要放进一页宽高的canvas里面,并对其余区域刷白
                        */
                        var ctx = canvas.getContext("2d");
                        let canvas_max_page_num = 9;
                        let canvas_current_page_num = 0;
                        var contentWidth = canvas.width;
                        var contentHeight = canvas.height;
                        //未生成pdf的html页面高度
                        var leftHeight = contentHeight;
                        //页面偏移
                        var position = 0;
                        //a4纸的尺寸[595.28,841.89],html页面生成的canvas在pdf中图片的宽高
                        var outputWidth = pw - 2 * marginw;//595.28//左右边距20
                        let rate = outputWidth / 780;//输出宽度和内容宽度的比例
                        var outputHeight = ph - 2 * marginh;//左右边距20
                        let onePdfPageInCanvasHeight = contentWidth/outputWidth*outputHeight;//一页pdf在canvas中的高度;
                        //var pageData = canvas.toDataURL('image/jpeg', 1.0);
                        var pageData = null;
                        var imgData = null;
                        var captureHeight = null;// 截取的当前canvas页的高度
                        let j = i;
                        while(position < contentHeight){
                            if (j++) {
                                pdf.addPage();
                            }
                            if(position+onePdfPageInCanvasHeight>contentHeight){
                                captureHeight = contentHeight-position;
                            }else{
                                captureHeight = onePdfPageInCanvasHeight;
                            }
                            imgData = ctx.getImageData(0,position,contentWidth,captureHeight);
                            pageData = ImageDataToUrl(imgData,onePdfPageInCanvasHeight);
                            pdf.addImage(pageData, 'JPEG', marginw, marginh, outputWidth, outputHeight);//左右边距20,被输出的图片会被拉伸为outputWidth,outputHeight的宽高
                            canvas_current_page_num++;
                            position += onePdfPageInCanvasHeight;
                            if (canvas_max_page_num > 0 && canvas_current_page_num >= canvas_max_page_num) {
                                break;
                            }
                        }
                        recursion(i + 1);
                    }
                })
            };
            recursion(rei);
        }
        //blockme();
        document.body.onload = function(){
            let i = 0;
            let Foo = function(){
                if ( i == 2) {
                    if(confirm("PDF已加载完毕,是否显示?")){
                        jsPdfDownload();
                    }
                }
            }
            AWSService.query(staticResourceContact.queryUrl, '{!quoteInfo.conAwsDataId}', function(data){
                //unblockUI();
                document.getElementById("accountContactName").innerHTML = data.object.lastName;
                document.getElementById("accountContactPhone").innerHTML = data.object.phone;
                document.getElementById("accountContactEmail").innerHTML = data.object.email;
                i++;
                Foo();
            }, staticResourceContact.token);
            AWSService.query(staticResourceOpportunity.queryUrl, '{!quoteInfo.oppAwsDataId}', function(data){
                //unblockUI();
                document.getElementById("dealerContactName").innerHTML = data.object.dealerSalesStaffName;
                i++;
                Foo();
            }, staticResourceOpportunity.token);
        }
</script>
<!-- 20220221 PI改造 by 徐亮 end  -->
</apex:page>