Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

285
Views
Enforce specific printing settings with javascript irrespective of the default settings of the user's printer

I am using a JavaScript function to print some contents. The function that I am using does the job correctly. The header, the footer and the title are set from the code. But I noticed that they are not being displayed in the preview. Also there are no margins left on all the four sides of the paper. Later when I checked the problem was associated with the default settings of the printer. I realised the paper was set to Letter and the header and footer was unchecked. So I had to manually do the settings to get the desired results.

So, I want to know is there a way to enforce the printing of the document the way I want even if the default of the user's printer is set to anything.

The below images show what I was getting and how I want to get it printed.

This is how it prints at default settings(I do not want like this.)This how I want to get it printed irrespective of the user's default printer settings.

Js function

$(function() {
    $.fn.printContent = function() {
      var target = $(this);
      var title;
      if (target.attr("title") != undefined) {
        title = target.attr("title");
      } else {
        title = "Element Content"
      }
      var uid = Date.now();
      var printFrame = $("<iframe>", {
        id: "printFrame_" + uid,
        name: "printFrame_" + uid
      }).css({
        position: "absolute",
        top: "-1000000px"
      }).appendTo($("body"));
      var frameHTML = $("<html>");
      $("<head>").appendTo(frameHTML);
      $("<title>").html(title).appendTo($("head", frameHTML));
      $("<body>").appendTo(frameHTML);
      if ($("body > link").length == 1) {
        $("body > link").clone().appendTo($("body", frameHTML));
      }
      $("body", frameHTML).append(target.html());
      var winFrame = window.frames['printFrame_' + uid];
      winFrame.document.open();
      winFrame.document.write(frameHTML.prop("outerHTML"));
      winFrame.document.close();
      setTimeout(function() {
        printFrame.focus();
        winFrame.print();
        printFrame.remove();
      }, 100);
    };
  
    $("#btnPrint").click(function() {
      $("#content").printContent();
    });
  });
  

Please suggest me how can I modify the above function to enforce the settings shown in the second image, like the paper size, margins, header and footer etc. from the function itself.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!