Как распечатать HTML-контент, включая JavaScript? - PullRequest
0 голосов
/ 15 апреля 2019

Я должен напечатать содержимое panelGrid, но p:printer не загружает код JavaScript.

Javascript работает на моей странице, но когда я, например, печатаю содержимое div, не загружаю javascript после нажатия кнопки «печать» ... на этом шаге: print

Что мне сделать, чтобы загрузить код javascript на принтер?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    template="/RESOURCES/template/layoutPrincipal.xhtml">

    <ui:define name="head">
        <link rel="stylesheet"
            href="${facesContext.externalContext.requestContextPath}/RESOURCES/css/paginaPrincipal.css"
            type="text/css" />
        <link rel="stylesheet"
            href="${facesContext.externalContext.requestContextPath}/RESOURCES/css/bootstrap.min.css"
            type="text/css" />
        <script type="text/javascript"
            src="${facesContext.externalContext.requestContextPath}/RESOURCES/js/bootstrap.min.js"></script>
        <script type="text/javascript"
            src="${facesContext.externalContext.requestContextPath}/RESOURCES/js/jquery.min.js"></script>

        <script type="text/javascript"
            src="${facesContext.externalContext.requestContextPath}/RESOURCES/js/jspdf.js"></script>

        <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>

        <style>
        @media print{
            * {
                box-sizing: border-box;
            }

            .img-zoom-container {
                position: relative;
            }

            .img-zoom-lens {
                position: absolute;
                border: 1px solid #d4d4d4;
                /*set the size of the lens:*/
                width: 200px;
                height: 180px;
                margin-left: 16%;
                margin-top: 4%;
            }

            .img-zoom-result {
                margin: auto;
                border: 1px solid #d4d4d4;
                /*set the size of the result div:*/
                width: 300px;
                height: 350px;
            }
        }
        </style>
        <script>

                function imageZoom(imgID, resultID) {
                  var img, lens, result, cx, cy;
                  img = document.getElementById(imgID);
                  result = document.getElementById(resultID);

                  lens = document.createElement("DIV");
                  lens.setAttribute("class", "img-zoom-lens");

                  img.parentElement.insertBefore(lens, img);

                  cx = result.offsetWidth / lens.offsetWidth;
                  cy = result.offsetHeight / lens.offsetHeight;
                  /*coloca as propriedades da DIV de resultado*/
                  result.style.backgroundImage = "url('" + img.src + "')";
                  result.style.backgroundSize = (img.width * cx) + "px " + (img.height * cy) + "px";

                  lens.addEventListener("click", moveLens);
                  img.addEventListener("click", moveLens);
                  /*para dispositivos touch*/
                  lens.addEventListener("touchmove", moveLens);
                  img.addEventListener("touchmove", moveLens);
                  function moveLens(e) {
                    var pos, x, y;

                    e.preventDefault();

                    pos = getCursorPos(e);
                                    x = pos.x - (lens.offsetWidth / 2);
                    y = pos.y - (lens.offsetHeight / 2);

                    if (x > img.width - lens.offsetWidth) {x = img.width - lens.offsetWidth;}
                    if (x &lt; 0) {x = 0;}
                    if (y > img.height - lens.offsetHeight) {y = img.height - lens.offsetHeight;}
                    if (y &lt; 0) {y = 0;}

                    lens.style.left = x + "px";
                    lens.style.top = y + "px";

                    result.style.backgroundPosition = "-" + (x * cx) + "px -" + (y * cy) + "px";
                  }
                  function getCursorPos(e) {
                    var a, x = 0, y = 0;
                    e = e || window.event;

                    a = img.getBoundingClientRect();

                    x = e.pageX - a.left;
                    y = e.pageY - a.top;

                    x = x - window.pageXOffset;
                    y = y - window.pageYOffset;
                    return {x : x, y : y};
                  }
                }               
            </script>

    </ui:define>

    <ui:define name="cont_principal">
        <h:form id="form">
            <p:panelGrid columns="1" style="width: 30%; float:left;">
                <p:dataTable id="singleDT" var="car"
                    value="#{dtSelectionView.cars2}" selectionMode="single"
                    selection="#{dtSelectionView.selectedCar}" rowKey="#{car.seqN}"
                    scrollRows="20" scrollable="true" liveScroll="true"
                    scrollHeight="500">
                    <p:column headerText="Serial">
                        <h:outputText value="#{car.seqN}" />
                    </p:column>
                    <p:column headerText="Data" style="width: 30%;">
                        <h:outputText value="#{car.data}" style="margin-right: 10%;" />
                    </p:column>
                    <p:column headerText="Hora">
                        <h:outputText value="#{car.hora}" />
                    </p:column>
                    <p:column headerText="Status">
                        <h:graphicImage url="#{car.infracao}" style="width: 15px; height: 15px;"/>
                    </p:column>
                    <f:facet name="footer">
                        <p:commandButton process="singleDT" update=":form:carDetail"
                            value="View" />

                        <p:commandButton update=":form:singleDT" value="Atualizar" />

                        <h:outputLabel value="Buscar: " />
                        <h:inputText id="search" value="#{dtSelectionView.search}"
                            style="margin-left: 7%;" />
                        <p:commandButton onclick="#{dtSelectionView.searchUpdate()}"
                            value="Buscar" style="visibility: hidden; display: none;" />

                        <p:commandLink style="margin: 2%; width: 15px; height: 15px;">
                            <h:graphicImage 
                                url="/RESOURCES/images/remoteAgent/bolaVerde.png" style="margin: 2%; width: 15px; height: 15px;"/>

                                <p:printer target=":form:carDetail"/>
                        </p:commandLink>

                    </f:facet>
                </p:dataTable>
            </p:panelGrid>

            <p:panelGrid id="carDetail" widgetVar="carPDF" columns="2"
                style="width: 70%; float: right;">

                <p:panelGrid columns="2" style="float: left; width: 20%;">
                    <h:outputText value="Serial:" />
                    <h:outputText value="#{dtSelectionView.selectedCar.seqN}" />

                    <h:outputText value="Placa:" />
                    <h:outputText value="#{dtSelectionView.selectedCar.plate}" />

                    <h:outputText value="Data:" />
                    <h:outputText value="#{dtSelectionView.selectedCar.data}" />

                    <h:outputText value="Hora:" />
                    <h:outputText value="#{dtSelectionView.selectedCar.hora}" />

                    <h:outputText value="Evento:" />
                    <h:outputText value="Verificar" />
                </p:panelGrid>
                <p:panelGrid columns="1" class="img-zoom-container">
                    <script>
                        // Initiate zoom effect:
                        imageZoom("myimage", "myresult");
                    </script>
                    <img id="myimage"
                        src="../../RESOURCES/images/remoteAgent/473_180420160608_HJF4013_2.jpg"
                        style="width: 70%; margin-top: 20px;" />

                    <div id="myresult" class="img-zoom-result"
                        style="width: 70%; align: center;"></div>                   
                </p:panelGrid>

            </p:panelGrid>          
        </h:form>
    </ui:define>
</ui:composition>

div "myresult" не загружается на страницу печати, потому что не загружен javascript?

...