Mark Stephens Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

PDF to HTML5 conversion – changing the page size

1 min read

By default, a we convert a PDF file into an HTML file with the same size. So a 200×100 pixel PDF file becomes a 200×100 pixel HTML file. You can easily scale in/out on most mobile devices but sometimes it is just too big or small as a sensible default.

So we have added a scaling setting into the latest release (so that you can have 400×200 pixels or 50×25 pixels). Note that the scaling has to be the same on both sized (if you turn a 200×100 pixel image into a 150x150pixel image, it will becomes ‘squashed’. It is important to maintain the aspect ratio and treat both sides the same.

Sometimes, however, we might want to scale the image up so that it makes the best fit into a different sized hole in our case, we would need to scale the image to 150x75pixels. This is very easy to do. All we need to do is calculate the scaling needed for height and width to fit the maximum gap and choose the smaller of the two values. Here is the sample code (which is also in our updated example). Happy scaling!

 

DynamicVectorRenderer HTMLoutput=new HTMLDisplay(page, midPoint, 
cropBox ,false, 100, new ObjectStore());

//have a scaling factor so we can alter the page size
float scaling=1.0f;

/**
 * if you want to fit it to a certain size, use this code
 * work out max possible scaling for both width and height
 * and use smaller to get max possible size but retain aspect ratio
 * - will not always be exact match as preserves aspect ratio
**/float preferredWidth=1000,preferredHeight=1000;

// scaling we need to scale w up to our value
float scalingX=preferredWidth/cropW; 
// scaling we need to scale w up to our value
float scalingY=preferredHeight/cropH; 

if(scalingX>scalingY)
   scaling=scalingY;
else
   scaling=scalingX;

//set page scaling (default is 100%)
HTMLoutput.setValue(HTMLDisplay.PercentageScaling, (int) (scaling*100));

Click here to see all the articles in the PDF to HTML5 conversion series.

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

Mark Stephens Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Converting your PDF files to HTML5 with BuildVu 

Recently we announced our updated product range for 2018 and are rebranding some existing products, like JPDF2HTML5 which has been renamed to BuildVu. It...
Georgia Ingham
2 min read

Favourite resources from our HTML development team

As the web progresses and grows, so do the technologies that come along with it. Trying to keep on top of everything you need...
Ovidijus Okinskas
1 min read

How HTML5 Javadocs in Java 9 will make your…

Here at IDRsolutions we are very excited about Java 9 and have written a series of articles explaining some of the main features. In...
Rob
1 min read

Leave a Reply

Your email address will not be published. Required fields are marked *