Home  • Download  • Register Now!  • Benefits  • Help  • Testimonials  • Samples  • Screenshots  • Awards  • Forum  • Convert Documents Online  • Print2Flash APIs  • Print2Flash vs. FlashPaper  • Contact Us
 Help Contents

Designing Custom Viewer for HTML5 Documents

Designing a viewer for HTML5 documents requires HTML and JavaScript programming and designing skills as document viewers are just regular HTML pages with linked page image SVG files.

Print2Flash SDK contains a sample of a simple document viewer for HTML5 documents. We recommend using this sample as a starting point when developing your own viewer. The description below deals with this sample code so refer to it for full details.

HTML5 document format

Print2Flash generates HTML5 documents in a special format. HTML5 documents are composed of:

  1. Document settings XML file
  2. Document folder with page SVG files and other support files (fonts, images, etc.)

Each page of a document is represented by a separate Scalable Vector Graphic (SVG) file. The files are named page1.svg, page2.svg, etc., the number suffix corresponding to the page number. You may load these SVG files in your own viewer for displaying to the user in any way you want. The SVG files are located in the document folder.

Additionally, Print2Flash generates a document file which is a XML file which contains all document settings which may help the viewer in rendering the document.

For example, a typical document may be composed of the following files and folders:

  • doc.xml - document settings file.
  • doc_files document folder:
    • page1.svg - the first page file;
    • page2.svg - the second page file;
    • page3.svg - the third page file;
    • Font1.woff - a font used by page files;
    • other support files.

Loading document settings

Before your viewer may start rendering the document, you need to load the document settings file. This file contains vital document information such as the total number of pages, width and height of each page, etc.

Settings are stored in a document settings XML file. To load a settings file in your viewer, you may use a hidden IFRAME tag:

<iframe id="Settings" style="display:none" src="doc.xml"></iframe>

You need to specify the URL of the document settings file in the SRC attribute.

To determine the moment when the your viewer and the settings file are loaded, you need to listen for onload event on the viewer's BODY tag:

<body onload="OnLoad()" ...>

Retrieving document settings

After the settings are loaded, you may retrieve document settings. Document may have a number of settings stored in the settings XML file. The list of common available settings is contained in Custom Document Viewer Settings help topic. To read these settings, first you need to retrieve the root node from the XML file and store it in a global variable:

function OnLoad() {
  docSettings = document.getElementById('Settings').contentWindow.document.documentElement.attributes;
  ...

To facilitate reading of settings, you may declare GetSetting function:

function GetSetting(name) 
{ 
  return docSettings.getNamedItem(name).value; 
}

After that you can use the declared GetSetting function to retrieve a setting value passing it the setting name as a parameter. For example, to get the number of pages of the document, you may use this code:

var pageNum=GetSetting("PageNum");

Rendering the document

After the settings are retrieved, you may start rendering the document. You may present the document to the user in any way you like it. The page image SVG files reside in the document folder and are named page1.svg for the first page of the document, page2.svg for the second page and so on. You may load these page image files in your viewer and use and display them as you like it. You may, for example, display only a single page at a time allowing a user to switch between pages with buttons. Or you may display the document in a booklet style. The approach you may choose is limited only by capabilities of web browsers and your imagination.

In our sample we demonstrate how a document can be presented as a vertical scrollable stack of pages as the standard Print2Flash document viewer does. To achieve this, we insert the document SVG page files in a scrollable DIV element:

<div id="DocArea" style="overflow: auto;"></div>

To insert the list of pages inside this tag, we create HTML code string composed of a sequence of IFRAME tags, each tag corresponding to each page. First, we retrieve widths and heights of each page as well as get a reference to the DIV container and initialize HTML code string variable (innerHTML):

var Widths = GetSetting('Widths', '').split(',')
var Heights = GetSetting('Heights', '').split(',')
DocArea = document.getElementById('DocArea');
var innerHTML = "";

Then, we need to enumerate each page and create HTML code for each page composed of an IFRAME tag:

for (var i = 1; i <= pageNum; i++) {
  var pageFile = "doc_files/page" + i + ".svg";
  innerHTML += "<iframe id='Page" + i + "' scrolling='no' style='border:10px solid gray;background-color:white;' width='" +
    Widths[i - 1] + "' height='" + Heights[i - 1] + "' src='" + pageFile + "'></iframe>";
}

Note that we set SRC attribute of IFRAME tags to the URLs of document pages which are stored in the document folder named (in this sample) doc_files. After the HTML code string is filled, we can insert it in the DIV container:

DocArea.innerHTML = innerHTML;

After that the browser should start loading document pages in IFRAME tags and display them in the scrollable container DIV element.

Accessing pages

If you need to access individual page IFRAME elements, you may retrieve a reference to each page with this code:

var page = document.getElementById("Page" + pageno)

This reference will point to an IFRAME object representing the page which number is specified by pageno variable.

If you need a reference to an SVG document object representing a page, you may use the following code to get it:

var svgpage = document.getElementById("Page" + pageno).contentDocument.documentElement

 Copyright © Print2Flash Software 2005-2017. All rights reserved.

Support | Privacy policy