flying saucer create blank page when set landscape? - java

I am using flying saucer to create pdf from html.
#page land { size: A4 landscape; }
.landscapePage {
page:land;
height: 14cm;
width: 30cm;
background-color: green;
}
<div class="landscapePage">
</div>
But i don't know why it created a blank page before landscape page. How do I avoid it?

Related

Click on generated button with Selenium+Java

This is site:
https://play.alienworlds.io/
I need to click on the login button.
In HTML I can't find it...
<body>
<div class="webgl-content">
<div id="unityContainer" style="width: 1528px; height: 355px; padding: 0px; margin: 0px; border: 0px; position: relative; background: url("Build/fff2a664dc06d7254246c6bb8d5d0e21.jpg") center center / cover;">
<canvas id="#canvas" style="width: 100%; height: 100%; cursor: default;" width="1528" height="355"></canvas>
<div class="logo " style="display: none;"></div>
<div class="progress Dark" style="display: none;">
<div class="empty" style="width: 0%;"></div>
<div class="full" style="width: 100%;"></div>
</div>
</div>
</div>
<script src="Build/061f463856577255fb5eefaf73e67127.js" type="text/javascript"></script>
<script src="bundle.js" type="text/javascript"></script>
<script src="hashes.js" type="text/javascript"></script>
<script src="message_handler.js" type="text/javascript"></script>
<script type="text/javascript">
// Initial screen setup
unityContainer.style.height = window.innerHeight + "px";
unityInstance = UnityLoader.instantiate(
"unityContainer",
"Build/bd2a2f07495f744fece3ee93c4a56908.json",
{ onProgress: UnityProgress }
);
function Resize() {
document.getElementById('unityContainer').style.width =
window.innerWidth + 'px';
document.getElementById('unityContainer').style.height =
window.innerHeight + 'px';
if (UnityLoader.SystemInfo.mobile) {
if (navigator.userAgent.indexOf('Android') != -1)
{
if (screen.width > screen.height) {
unityInstance.SendMessage("Canvas", "SetScreenModePanel", "false");
}
else
{
unityInstance.SendMessage("Canvas", "SetScreenModePanel", "true");
}
}
else
{
switch (window.orientation) {
case -90:
case 90:
unityInstance.SendMessage('Canvas', 'SetScreenModePanel', 'false');
break;
default:
unityInstance.SendMessage('Canvas', 'SetScreenModePanel', 'true');
break;
}
}
}
}
/*function Fullscreen() {
unityInstance.SetFullscreen(1);
if (navigator.userAgent.indexOf('Android') != -1)
window.screen.orientation.lock('landscape');
}*/
window.addEventListener('orientationchange', Resize);
// Initial execution if needed
//Resize();
</script>
<script src="blob:https://play.alienworlds.io/9ffeac95-ddb2-480b-a75f-a20043229a5b" id="0941210686ad38c201cd5aecd353ebd4"></script>
</body>
The question is tricky.
I've added the answer in Python. It uncovers the concept. If you can't, I will translate the answer to Java. The approach to such tasks is really what you need.
I could click login button with ActionChains class.
Please not that this is not very stable case for few reasons:
1 It depends on screen resolution, so you should set the resolution after your browser is opened
2 You will need to add waits (replace time.sleep() with Selenium's waits)
3 You cannot move your mouse while test is executing because it will break your actions
4 And the biggest problem on my opinion is that after you click the button, you will be dealing with Captcha.
This is my code which I used while debugging. You should set screen resolution because most probably your coordinates will be different.
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
url = "https://play.alienworlds.io/"
driver.get(url)
driver.set_window_size(1522, 754) # setting window size to be sure what resolution you work with
time.sleep(20)
driver.implicitly_wait(25)
canvas = driver.find_element_by_css_selector("canvas[id='#canvas']")
print(driver.get_window_size()) # print to make sure window size is correct
drawing = ActionChains(driver)
print("moving by offset")
drawing.move_by_offset(450, 511)
print("clicking")
drawing.click()
print("clicked")
drawing.perform()
print("performed")
time.sleep(5) # just to check that a new window is opened
driver.close()
driver.quit()
I could improve it, but it is not 5 minutes task.
Good luck!
p.s. move_by_offset moves the cursor from the upper left corner of your screen.
Update:
I've added driver.set_window_size(1522, 754) This is the canvas resolution I get when I inspect canvas element.
I print print(driver.get_window_size()) to make sure the screen resolution is as expected.
I've tried few tools to measure browser window size and found out that My Ruler extension by Leann Pagac works the best for canvas. All other extensions, even they are more commonly used could not measure coordinates on the canvas correctly.
Also, check this post to get more details on how to work with canvas How to perform Mouse Wheel scrolling over HTML5 Canvas in Selenium?

Simple way to display currency symbol in html2pdf for iText 7

I updated my code from iText 5.0 to iText 7 and html2pdf 2.0 according to this post. In earlier version rupee symbol was rendering properly, but because of css issue i changed the code. Now complete page is converting properly to pdf except rupee symbol.
Tried adding font in html style tag itself like * { font-family: Arial; }.
Changed value of rupee symbol from &#x20b9, ₹ and also added directly ₹ , but no use.
My Html:
<html>
<head>
<style>
* { font-family: Arial; }
</style>
<title>HTML div</title>
</head>
<body>
<p style="margin-bottom: 0in; padding-left: 60px;">
<div style="font-size: 450%; text-indent: 150px;">
<strong>BUY <span style="color: #ff420e;">2</span> GET
</strong>
</div>
</p>
<div
style="float: left; display: inline-block; margin: 10px; text-align: right; font-size: 70%; line-height: 27; transform: rotate(270deg);">Offer
Expiry Date : 30/11/2017</Div>
<div
style="float: left; display: inline-block; margin: 10px; text-align: right; font-size: 350%;">
₹
<!-- ₹ -->
</div>
<div
style="float: left; display: inline-block; margin: auto; font-size: 1500%; color: red; font-weight: bold;">99</div>
<div
style="float: left; display: inline-block; margin: 10px; text-align: left; font-size: 250%; line-height: 10;">OFF</div>
<div
style="position: absolute; height: 40px; font-size: 250%; line-height: 600px; color: red; text-indent: 50px">Pepsi
2.25 Pet Bottle ltr</div>
<div
style="position: absolute; height: 40px; font-size: 245%; line-height: 694px; text-indent: 50px">
MRP: ₹ <span style="color: #ff420e;">654</span>
</div>
</body>
</html>
Java Code :
public class Test {
final static String DEST = "D://Workspace_1574973//POP//sample_12.pdf";
final static String SRC = "D://Workspace_1574973//POP//src//com//resources//test.html";
public static void main(String[] args) throws Exception {
createPdf(SRC, DEST);
}
public static void createPdf(String src, String dest) throws IOException {
HtmlConverter.convertToPdf(new File(src), new File(dest));
}
}
Earlier code, which was working with symbols.
log.info("Creating file start");
OutputStream file = new FileOutputStream(new File("font_check.pdf"));
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
InputStream is = new ByteArrayInputStream(fileTemplate.getBytes());
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
document.close();
file.close();
log.info("Creating file end");
Is there any simple approach to achieve this, with minimal and optimized code ?
Because I've to generate thousands of pdf in one go, So the performance should not affect.
Please let me know, if anyone achieved this through latest version.
Edit : Also how to set particular paper type in this like A6, A3, A4 etc.
Hope you are not mad, because I don't have reputation to write simple comments... so I'll post a full answer instead. I parse HTML for my work, and I read SO sometimes. There is a lot on the subject regarding UTF-8 here. Most software systems support the "greater than char #256" (UTF-8) codes - for instance the Indian Rupee Symbol. However, most of the time the programmer has to include a specific request for such a desired behavior, explicitly.
In HTML, for instance - adding this line usually helps:
String UTF8MetaTag = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
Anyway, not having used HTMLToPDF - I might not be the right guy to post answers to your questions - but, because I have dealt with UTF-8 foreign language characters for three years, I know that setting a software setting to handle the 65,000 or so chars is usually VERY EASY, BUT ALSO ALWAYS VERY MANDATORY.
Here is an SO post about using HTMLToPDF and UTF-8 to handle Japanese Kanji characters. Most likely, it should handle all UTF-8, but that is not a guarantee.
HTML2PDF support for japanese language(utf8) is not working
Here are a few posts about it using HTML2PDF in PHP:
Converting html 2 pdf (php) using hebrew returns "???"
Having æøå chars in HTML2PDF charset

Flying Saucer - html entities are not rendered

I'm generating pdf using flying-saucer lib. But I have problem with some html entities.
I've already was searching for solution I found many tips in this forum, and in other places but still there is the problem.
I've tried this approach :
http://sdtidbits.blogspot.com/2008/11/flying-saucer-xhtml-rendering-and-local.html
but without any success
My code look like this:
os = new FileOutputStream(pdf);
ITextRenderer renderer = new ITextRenderer();
ChainingReplacedElementFactory chainingReplacedElementFactory = new ChainingReplacedElementFactory();
chainingReplacedElementFactory.addReplacedElementFactory(new B64ImgReplacedElementFactory(renderer.getSharedContext()));
renderer.getSharedContext().setReplacedElementFactory(chainingReplacedElementFactory);
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
where pdf is the name of new pdf to create and url is
File f = new File(url);
if (f.exists()) {
url = f.toURI().toURL().toString();
}
my html file look like this
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org" />
<style type="text/css">
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, caption, tbody, tfoot, thead, tr, th
{
color: #444;
font-family: Arial;
font-size: 14px;
line-height: 25px;
border: none;
}
table, td {border: solid 1px #CCC;}
img {page-break-inside: avoid;}
</style>
<title></title>
</head>
<body>
<h1>Test</h1>
<p>Html etites to test</p>
<p>←</p>
<p>←</p>
<p>↑</p>
<p>↑</p>
<p>↓</p>
<p></p>
</body>
</html>
Everything works fine beside those entities. There is nothing rendered only blank spots where should by arrows.
Does anyone has solution for that ?
The issue is that the font used by iText by default doesn't support the caracters you want to print.
The solution is to embed another font which can display this character, for example DejaVu.
In the java file, declare the font to the renderer:
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont("font/DEJAVUSANS.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
And change the font-family declaration in the HTML:
body
{
font-family: DejaVu Sans;
}

Cleaning up my customLabel with CSS

I am customizing a label and placed the name of the label in the one cell of a grid and an asterisk(image) in the next cell.
<ui:image field="requiredImage" src="images/required_indicator.gif"/>
<ui:style type="com.equillore.mcmexternal.client.ui.IndicatorLabel.Style">
#sprite
.requiredAsterisk
{
gwt-image: 'requiredImage';
width: 7px;
height: 14px;
}
.labRequired
{
color:#B22222;
font-size:14;
font-style:normal;
font-weight:bold;
font-family: serif;
letter-spacing: 0.07em;
}
</ui:style>
<g:Grid >
<g:row>
<g:customCell>
<g:Label ui:field="label" addStyleNames="{style.labRequired}"/>
</g:customCell>
<g:customCell>
<g:Label addStyleNames="{style.requiredAsterisk}"/>
</g:customCell>
</g:row>
</g:Grid>
when I run this the asterisk image is however displayed in front of the label(labRequired) as well just after it. So it is displayed twice.
why is this happening and how can I get rid of the 'extra' astersik image?
Kind regards
<ui:image field="requiredImage" src="images/required_indicator.gif"/>
This line is actually inserting the image in the UIBinder. Remove it.
Instead add it as a managed resource in the client bundle.
https://developers.google.com/web-toolkit/doc/latest/DevGuideClientBundle#Image_Sprites

Regarding the Link / text / image that stays static even when the webpage is scrolled

are there any Java script or any sample available for the Link / text / image that stays static even when the webpage is scrolled.
I am looking for something similar to the one on the bottom left side of the webpage borders(Helpful?, "Yes", "No").
http://www.ehow.com/facebook-for-business/
Regards,
Gourav
Try this code this may help you. but this code required lot more css and javascript code to make this same as ehow.
<style>
.mainDiv {
height: 1000px;
border: solid 1px #000000;
}
.fixDiv {
height: 250px;
width: 20px;
border: solid 1px #000000;
position: fixed;
margin-top: 200px;
}
</style>
<html>
<body>
<div class="fixDiv">h e l p f u l l? Yes No</div>
<div class="mainDiv">Gaurav</div>
</body>
</html>
This can be done using simple css
create a style for div with id="poll" and give this style
div#poll {
position: fixed;
left: 0;
top: 100px;
}
<div id="poll">
<img src="image.jpg" alt="image" class="contimage" border="0"/></div>
This div will be shown in the left side of the window

Categories