buli
2022-05-13 2f4492ee18f90274582fcc2bb06f5e9bf64136e8
force-app/main/default/pages/QuotesPDF.page
@@ -1,4 +1,4 @@
<apex:page controller="QuotesPDFController" showHeader="false" sidebar="false"  id="allPage" action="{!init}" >
<apex:page controller="QuotesPDFController" showHeader="false" sidebar="false"  id="allPage" action="{!init}" renderAs="pdf">
<!-- renderAs="pdf" -->
      <html>
@@ -8,30 +8,11 @@
                        size: A4 landscape;
                        margin: 12mm 12mm 5mm 12mm;
                    }
         /* 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  -->
            </head>
            <body>
                     <div id="pdf-wrapper">
                         <div id="title1"></div>
                  <table border="0" style="font-family: Arial Unicode MS; page-break-inside: auto" width="100%">
                        <colgroup>
                      <col width="60%"/>
@@ -73,14 +54,14 @@
                              <td>
                                  <!-- <c:PDFWbr targetStr="{!conName}"/><br/>
                                  <c:PDFWbr targetStr="{!quo.COMPANYName__c}"/><br/> -->
                                  <p style="line-height:15px;"><apex:outputField id="BILL_TO__c" value="{!quo.BILL_TO__c}"/><br/>
                                  <p style="line-height:15px;"><apex:outputField value="{!quo.BILL_TO__c}"/><br/>
                                  CHINA
                                  </p>
                              </td>
                              <td>
                                  <!-- <c:PDFWbr targetStr="{!conName}"/><br/>
                                  <c:PDFWbr targetStr="{!quo.COMPANYName__c}"/><br/> -->
                                  <p style="line-height:15px;"><apex:outputField id="SHIP_TO__c" value="{!quo.SHIP_TO__c}"/><br/>
                                  <p style="line-height:15px;"><apex:outputField value="{!quo.SHIP_TO__c}"/><br/>
                                  CHINA
                                  </p>
                              </td>
@@ -127,14 +108,14 @@
                        </apex:outputPanel>
                        <apex:outputPanel layout="none" rendered="{!IF(conName != null, true, false)}">
                              <td style="border-bottom-width: 0;" id="conName">{!conName}&nbsp;</td>
                              <td style="border-bottom-width: 0;">{!conName}&nbsp;</td>
                        </apex:outputPanel>
                        <apex:outputPanel layout="none" rendered="{!IF(conName != null, false, true)}">
                              <td style="border-bottom-width: 0;">&nbsp;</td>
                        </apex:outputPanel>
                        <apex:outputPanel layout="none" rendered="{!IF(quo.CONTACT_PHONE__c != null, true, false)}">
                              <td style="border-bottom-width: 0;" id="CONTACT_PHONE__c">{!quo.CONTACT_PHONE__c}&nbsp;</td>
                              <td style="border-bottom-width: 0;">{!quo.CONTACT_PHONE__c}&nbsp;</td>
                        </apex:outputPanel>
                        <apex:outputPanel layout="none" rendered="{!IF(quo.CONTACT_PHONE__c != null, false, true)}">
                              <td style="border-bottom-width: 0;">&nbsp;</td>
@@ -317,132 +298,8 @@
                              </apex:outputText></td>
                  </tr>
                  </table>
                              </div>
            </body>
                              <!-- 20220221 PI改造 by 徐亮 start  -->
<script>
    AWSService.sfSessionId = '{!GETSESSIONID()}';
    var staticResources = JSON.parse('{!staticResource}');
    var contactStaticResource = JSON.parse('{!contactStaticResource}');
    function Fun(pdf){
          let uri = pdf.output('datauristring');
          let matches = /(?<=addFile=)[^&]*/.exec(window.location.href);
          if (matches && matches.length) {
                eval('top.'+matches[0]+'("'+uri+'")');
                return;
          }
          var iframe = document.createElement('iframe');
          iframe.setAttribute('style', 'position:absolute;right:0; top:0; bottom:0; height:100%; width:100%');
          document.body.appendChild(iframe);
          iframe.src = uri;
          for (let e of document.body.childNodes) {
                if (e != iframe && e.style) {
                      e.style.display = 'none';
                }
          }
    }
    let id = "pdf-wrapper";
    var target = document.getElementById(id);
    function jsPdfDownload(){
        let pdfName = "测试";
        let id = "pdf-wrapper";
        var target = document.getElementById(id);
        target.style.background = "#FFFFFF";
        if(pdfName==''||pdfName==undefined) pdfName= getNowFormatDate();
        html2canvas(target, {
            scale: 2,
            onrendered:function(canvas) {
                let landscape = false;
                let pw = 595.28;
                let ph = 841.89;
                const marginw = 40;
                let orientation = '';
                if (landscape) {
                    let temp =ph;
                    ph = pw;
                    pw = temp;
                    orientation = 'l';
                }
                var contentWidth = canvas.width;
                var contentHeight = canvas.height;
                //一页pdf显示html页面生成的canvas高度;
                var pageHeight = contentWidth / pw * ph;
                //未生成pdf的html页面高度
                var leftHeight = contentHeight;
                //页面偏移
                var position = 0;
                //a4纸的尺寸[595.28,841.89],html页面生成的canvas在pdf中图片的宽高
                var imgWidth = pw-2*marginw;//595.28//左右边距20
                var imgHeight = imgWidth/contentWidth * contentHeight;//左右边距20
                var pageData = canvas.toDataURL('image/jpeg', 1.0);
                var pdf = new jsPDF(orientation, 'pt', 'a4');// 第一个参数填字母l,注意不是数字1,为横向pdf
                //有两个高度需要区分,一个是html页面的实际高度,和生成pdf的页面高度(841.89)
                //当内容未超过pdf一页显示的范围,无需分页
                if (leftHeight < pageHeight) {
                    pdf.addImage(pageData, 'JPEG', marginw, 0, imgWidth, imgHeight );//左右边距20
                } else {
                    while(leftHeight > 0) {
                        pdf.addImage(pageData, 'JPEG', marginw, position, imgWidth, imgHeight)//左右边距20
                        leftHeight -= pageHeight;
                        position -= ph;
                        //避免添加空白页
                        if(leftHeight > 0) {
                            pdf.addPage();
                        }
                    }
                }
                Fun(pdf);
            }
        })
    }
    document.body.onload = function(){
        let i = 0;
        let Foo = function(){
              if(i == 2){
                  jsPdfDownload();
              }
        }
        if('{!quo.AWS_Data_Id__c}'){
            AWSService.query(staticResources.queryUrl, '{!quo.AWS_Data_Id__c}', function(data){
                  let e = document.getElementById("allPage:BILL_TO__c");if(e)e.innerHTML = data.object.billTo;
                  e =document.getElementById("allPage:SHIP_TO__c");if(e)e.innerHTML = data.object.shipTo;
                  e =document.getElementById("CONTACT_PHONE__c");if(e)e.innerHTML = data.object.contactPhone;
                  i++;
                  Foo();
            }, staticResources.token);
        }else{
              i++;
              Foo();
        }
        if('{!quo.CONTACT_NAME__r.AWS_Data_Id__c}'){
            AWSService.query(contactStaticResource.queryUrl, '{!quo.CONTACT_NAME__r.AWS_Data_Id__c}', function(data){
                  let e = document.getElementById("conName");if(e)e.innerHTML = data.object.lastName;
                  i++;
                  Foo();
            }, staticResources.token);
        }else{
              i++;
              Foo();
        }
    }
</script>
<!-- 20220221 PI改造 by 徐亮 end  -->
      </html>
</apex:page>