Viewing post

Woha – nearly a year! CKEditor and A4 paper size.

Nearly a year since I last blogged. Phew, caught it just in time ;-)

First time this year (2013) I got to drive home without my lights on. The clocks haven’t even gone back yet! Woo-hoo for springtime :)

So today’s topic, CKEditor and A4 paper. Mmm-hmm. Letter paper, A4, A5, in fact any size. You want to let someone WYSIWYG edit layout on a piece of paper, envelope, acetate, banner, badge or whatever. Sure, we can do that!

<script type="text/javascript">
// ON CKEDITOR READY:
CKEDITOR.on('instanceReady', function() {;
var iframe = $('#cke_editable_area iframe').contents ();
iframe.find ('html').css ({
'background-color': '#b0b0b0'
});
iframe.find ('body').css ({
'width': '297mm',
'height': '210mm',
'background-color': '#ffffff',
'margin': '0mm',
'padding': '5mm'
});
});</script>

Yee-haa cowboy. The above uses the CKEditor instanceReady event and some jQuery magic (not optimally written :) ) to force the width/height of the editable area. Unfortunately text can flow past the bottom (but not off the sides or top) – but that’s probably a good thing, else the user would not “see” that they’ve made a mistake.

Some grey colouring gives the illusion that you’re working with your favourite text editor.

Shove the result to a fresh page with the following CSS for printing, certainly with Chrome – works like a champ. (Note I’m using A4 measurements in the JavaScript, so the CSS below is to match that.)

body {
font-family: sans-serif, Arial, Verdana, "Trebuchet MS";
width: 297mm;
height: 210mm;
background-color: #ffffff;
margin: 0mm;
padding: 0mm;
}

@page {
size: landscape;
width: 297mm;
height: 210mm;
margin: 5mm;
}

UPDATE: further information on how I achieved this has been requested … what I ended up with (after much fiddling) was a 95% accurate “what you see is what will print” … using a kludge of ugly tricks.

The main JS:

<script type="text/javascript">
// ON CKEDITOR READY: Sets up page size and white colour background in edit area
CKEDITOR.on('instanceReady', function() {;
	var iframe = $('#cke_editable_area iframe').contents ();
	iframe.find ('html').css ({
		'background-color': '#b0b0b0'
	});
	iframe.find ('body').css ({
		'width': '297mm',
		'height': '420mm', //was 210 ****
		'background-color': '#ffffff',
		'margin': '0mm',
		'padding': '5mm'
	});
	iframe.find ('td').css ({
		'padding': '-1px'
	});
});</script>

That generates a landscape A4 page, editable in your browser.

To print, I pass the content (HTTP POST, basically) into a printable page:

<html lang="en" dir="ltr">
   <!-- the following stylesheet is provided by ckeditor - and provides some sane defaults for displaying -->
   <link rel="stylesheet" type="text/css" href="/css/ckeditor.css" />
   <body
     class="cke_contents_ltr cke_show_borders"
     spellcheck="false"
     style="width: 297mm;
       height: 420mm;
       background-color: rgb(255, 255, 255);
       margin: 0mm;
       padding: 5mm;
       cursor: auto;">
      <table
        class="cke_show_border"
        cellspacing="0"
        cellpadding="0"
        style="width:1120px; border:none;"
        _moz_resizing="true">
         <?php print $contents; ?>
      </table>
   </body>
</html>

The CSS referenced is vanilla from CKEditor.

7 Responses to “Woha – nearly a year! CKEditor and A4 paper size.”

  1. Joe Says:

    This is exactly what I am trying to do!

    Do you have a full example as I can’t get your above code to work properly.

    Thanks in advance

  2. wally Says:

    Hi Joe,

    Yeah – actually I do. But they’re unfortunately behind a secured intranet.

    Had a lot of issues getting what was generated to print out “as desired” (i.e. 100% what-you-see-is-what-you-print) … but >95% I’d say.

    I’ll update the post in the next half hour with as much detail as I can pull (it was a while ago I worked on it!) – feel free to let me know if you have any further issues getting it to work.

  3. Valere Says:

    Hi Joe, very useful post – I’m using this on a project I’m working on. I have a problem though. If I click on the ‘source’ button, the border disappear. How do you cope with that?

  4. Penelope Says:

    It’s hard to find your articles in google. I found it on 17 spot, you should build quality
    backlinks , it will help you to get more visitors.
    I know how to help you, just search in google – k2 seo tips
    and tricks

  5. Matthew Says:

    I read a lot of interesting articles here. Probably you spend a lot of time writing,
    i know how to save you a lot of time, there
    is an online tool that creates unique, google friendly articles
    in seconds, just search in google – laranitas free content source

  6. http://www.161993up.com Says:

    I’m gone to convey my little brother, that he should also pay a quick visit this blog
    on regular basis to obtain updated from hottest news.

    Feel free to surf to my website :: http://www.161993up.com

  7. {buy pinterest followers Says:

    This post is priceless. Where can I find out more?

    Chheck out mmy web site … {buy pinterest followers

Leave a Reply