Chrome — проблема с выводом на печать

Приветствую!

Задача: необходимо выводить на печать некий текст с помощью JS.

Создаем iframe вставляем туда текст, и выводим диалоговое окно печати: frames['frame-print'].print();

Все вроде бы работает, жмем нашу кнопку появляется окно печати, жмем кнопку Печать и все готово.

Но в Chrome наблюдается такая проблема, если если закрыть диалоговое окно крестом и или кнопкой Отмена, то при повторном нажатии на печать (js) получаем сообщение: Ignoring too frequent calls to print().

Где-то через минуту начитает опять работать.

Можно ли это как-то обойти или вылечить?
  • Вопрос задан
  • 6509 просмотров
Решения вопроса 1
wearymax
@wearymax
Попробуйте так:

Скрипт:
Copy Source | Copy HTML
  1. function PrintIt(){
  2.     var ua=navigator.userAgent;
  3.     var ie=/MSIE/.test(ua);
  4.     stext='';
  5.     stext=document.getElementById("Printable").innerHTML;
  6.     wnd=window.open("", "tinyWindow", 'statusbar=no,toolbar=no,scrollbars=yes,resizable=yes,width=630,height=900');
  7.     wnd.document.write("<html>
    <title>Печать страницы</title>
    <head>
    <link href=\"/style/print.css\"rel=\"stylesheet\"type=\"text/css\" media=\"all\"/></style>
    </head>
    <body onclick=\"window.close()\">
    <div id=\"watermark-top\">начало листа</div>");
  8.     wnd.document.write(stext);
  9.     if (!ie){
  10.     wnd.document.write("<div id=\"watermark-bottom\">конец листа</div><body></html>");
  11.     wnd.print();
  12.     }else{
  13.     wnd.document.write("<script>window.onload=self.print();<\/script></body></html>");
  14.     wnd.location.reload()
  15.     }
  16. }

(Уберите переносы строки у всех элементов в wnd.document.write)

Область и вызов:
Copy Source | Copy HTML
  1. <div id="Printable">Контент для печати</div>
  2. <button onclick="PrintIt();">Печать</button>
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
GomelHawk
@GomelHawk
PHP / Symfony developer
src.chromium.org/viewvc/chrome/trunk/src/chrome/renderer/print_web_view_helper_win.cc?revision=27421&view=markup&pathrev=31561

По этому адресу видим:


  // TODO(maruel): Move this out of platform specific code.
  // Check if there is script repeatedly trying to print and ignore it if too
  // frequent.  We use exponential wait time so for a page that calls print() in
  // a loop the user will need to cancel the print dialog after 2 seconds, 4
  // seconds, 8, ... up to the maximum of 2 minutes.
  // This gives the user time to navigate from the page.
  if (script_initiated && (user_cancelled_scripted_print_count_ > 0)) {
    base::TimeDelta diff = base::Time::Now() - last_cancelled_script_print_;
    int min_wait_seconds = std::min(
        kMinSecondsToIgnoreJavascriptInitiatedPrint <<
            (user_cancelled_scripted_print_count_ - 1),
        kMaxSecondsToIgnoreJavascriptInitiatedPrint);
    if (diff.InSeconds() < min_wait_seconds) {
      WebString message(WebString::fromUTF8(
          "Ignoring too frequent calls to print()."));
      frame->addMessageToConsole(WebConsoleMessage(
          WebConsoleMessage::LevelWarning,
          message));
      return;
    }
  }


Как я понял — там жестко забита задержка…
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы