0

I am trying to print the contents of a specific div with a class called "container" which has a number of divs having their individual classes. The problem I am facing is that when I click the print button the print preview shows the contents to be printed in completely plain format, no css styles applied. But I want the contents to be exactly the way it is being displayed in the browser. I have tried a number of ways and solutions from here but nothing seems to work. Please suggest me some way to do it. I was trying to do it with javascript. The javascript code given below is cpied as I said I was trying out all the possible ways.

javascript

$(function () {
 $("#btnPrint").click(function () {
 var contents = $("#content").html();
 var frame1 = $('<iframe />');
 frame1[0].name = "frame1";
 frame1.css({ "position": "absolute", "top": "-1000000px" });
 $("body").append(frame1);
 var frameDoc = frame1[0].contentWindow ? frame1[0].contentWindow : frame1[0].contentDocument.document ? frame1[0].contentDocument.document : frame1[0].contentDocument;
 frameDoc.document.open();
 //Create a new HTML document.
 frameDoc.document.write('<html><head><title>DIV Contents</title>');
 frameDoc.document.write('</head><body>');
 //Append the external CSS file.
 frameDoc.document.write('<link href="C:\Users\Intel\Envs\test\project\static\doc.css" rel="stylesheet" type="text/css" />');
 //Append the DIV contents.
 frameDoc.document.write(contents);
 frameDoc.document.write('</body></html>');
 frameDoc.document.close();
 setTimeout(function () {
 window.frames["frame1"].focus();
 window.frames["frame1"].print();
 frame1.remove();
 }, 500);
 });
});

The below images are the way it is being displayed in the browser and the way it is displayed in the preview

This is how i want it to get it printed

this is how want to get it printed and this is how it getting printed]2

html file

<link rel="stylesheet" href="{% static 'doc.css' %}" media="all"/>
<div class="main-container">
 <div class="container" id="content"> 
 
 {% if object.reportable %}
 <div class="report">
 <p>Reportable </p>
 </div>
 {% endif %}
 {% if object.non_reportable %}
 <div class="report">
 <p>Non-Reportable </p>
 </div>
 {% endif %}
 {% if object.over_ruled %}
 <div class="over">
 <p>Over Ruled </p>
 </div>
 {% endif %}
 {% if object.equivalent_citations %}
 <div class="cit">
 <p><b>Equivalent Citations:</b> {{object.equivalent_citations}}</p>
 </div>
 {% endif %}
 <div class="crt">
 <p><b>In The {{object.court_type}}</b></p>
 </div>
 <div class="appeal">
 <p><b>{{object.apelLate_type}}</b></p>
 </div>
 <div class="jdge">
 <p> <b>Before:</b> {{object.judge_name}}</p>
 </div>
 <div class="party-name">
 <p> <b> {{object.party_name}} </b> </p>
 </div>
 
 <div class="case-no">
 <p><b>Case No.:</b> {{object.case_no}} </p>
 </div>
 ...
 
 <div class="container-2">
 <input type="button" id="btnPrint" value="Print" />
 </div>
</div>
asked Sep 21, 2021 at 16:55
8
  • You appear to be calling on a CSS Fle that is on the Local hard drive: 'C:\Users\Intel\Envs\test\project\static\doc.css' and I suspect the browser does not have permission to call this file. You might need to call a URL or URL and not a File Path. Commented Sep 21, 2021 at 17:01
  • 1
    The browser likely isn't able to access C:\Users\Intel\... -- use a path within your webroot. Commented Sep 21, 2021 at 17:01
  • This means that the css file need to be hosted first? otherwise is the javascript code correct ? Commented Sep 21, 2021 at 17:28
  • But if the browser is not able to access the file or permission to access the file then how it is rendering the styles inside the div? Commented Sep 21, 2021 at 17:31
  • @RitankarBhattacharjee most likely this is a relative path inside the Web Server or calling a URL Path. Please examine the HREF link in your primary document. Commented Sep 21, 2021 at 17:36

2 Answers 2

1

Consider the following example: https://jsfiddle.net/Twisty/aspehr0m/7/

JavaScript

$(function() {
 $("#btnPrint").click(function() {
 var contents = $("#content").html();
 var frame1 = $('<iframe>', {
 id: "frame1",
 name: "frame1"
 })
 .css({
 "position": "absolute",
 "top": "-1000000px"
 })
 .appendTo($("body"));
 var myHTML = $("<html>");
 $("<head>").appendTo(myHTML);
 $("<title>").html("DIV Contents").appendTo($("head", myHTML));
 $("<body>").appendTo(myHTML);
 $("body > link").clone().appendTo($("body", myHTML));
 $("body", myHTML).append(contents);
 console.log("Content", myHTML.prop("outerHTML"));
 var frameDoc = window.frames.frame1;
 frameDoc.document.open();
 //Create a new HTML document.
 frameDoc.document.write(myHTML.prop("outerHTML"));
 frameDoc.document.close();
 setTimeout(function() {
 frame1.focus();
 window.frames.frame1.print();
 frame1.remove();
 }, 500);
 });
});

Here you can use .clone() to make a copy of the existing Stylesheet Link. this will ensure it uses the same as the primary page.

It is consider a better practice not to mix JavaScript and jQuery, to stick to one or the other. In this case, it's a bit easier to to manage the iFrame element with native JavaScript.

Update

You might consider making a new Function: https://jsfiddle.net/Twisty/aspehr0m/38/

JavaScript

$(function() {
 $.fn.printContent = function() {
 var target = $(this);
 var title;
 if (target.attr("title") != undefined) {
 title = target.attr("title");
 } else {
 title = "Element Contents"
 }
 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();
 });
});
answered Sep 21, 2021 at 19:27
Sign up to request clarification or add additional context in comments.

3 Comments

This function worked perfectly. Thank you so much for the help sir :). This completely solved my problem.
Hey, @Twisty I need one more help with this fuction. Will you please help me???
@RitankarBhattacharjee I would advise trying to work it out yourself first. Google or check other posts that are similar. If you still need help, post another question with a proper example.
0

Use an URL or accessible path instead of:

C:\Users\Intel\Envs\test\project\static\doc.css" rel="stylesheet" type="text/css" />

By the way, The javascript seems to be ok, your page wouldn't show content if it were misspelled

answered Sep 21, 2021 at 17:37

1 Comment

okay, i shall host it and then try to print it. Thank you.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.