Strange getting width of page in phantomjs - phantomjs

here is the script code:
var page = require('webpage').create();
page.paperSize = {
format: 'A4',
orientation: "landscape"
};
page.open('http://www.google.com', function () {
var arr = page.evaluate(function () {
var pageWidth = document.body.clientWidth;
var pageHeight = document.body.clientHeight;
return [pageWidth, pageHeight];
});
console.log('pagwWidth: ' + arr[0] + '; pageHeight: ' + arr[1]);
page.render('google.pdf');
phantom.exit();
});
I'm trying to get clientWidth and clientHeight of document.body page. When I exec this script I'm getting the folowing values:
pagwWidth: 400; pageHeight: 484
Why is the width above is 400px? I think I should be wider.
Thank you for the reply. But then I don't understand the following thing. When I use viewportSize = {width: 1024, height: 800}
var page = require('webpage').create();
page.paperSize = {
format: 'A4',
orientation: "landscape"
};
page.viewportSize = {width: 1024, height: 800};
page.open('http://www.google.com', function () {
page.render('google.pdf');
phantom.exit();
});
I get the following file:
But if I use viewportSize = {width: 400, height: 400}
var page = require('webpage').create();
page.paperSize = {
format: 'A4',
orientation: "landscape"
};
page.viewportSize = {width: 400, height: 400};
page.open('http://www.google.com', function () {
page.render('google.pdf');
phantom.exit();
});
I get the same:
So I don't understand how does viewportSize affect to the view?

The document is affected by the viewport size and not by the paper size. Think along this line, how a web page looks like in your web browser has nothing to do with your current printer setting.
Use viewportSize if you want to influence the page layout:
page.viewportSize = { width: 1024, height: 800 };

Related

how to create pdf file in react-native?

error : null is not an object evaluating '_PDFLib.default.createPDF'
https://github.com/Hopding/react-native-pdf-lib
I use the pdf library, can you tell me why the error occurs?
If you use await(const docsDir = await PDFLib.getDocumentsDirectory();), you get an error message like this: error - Can not use keyword 'await' outside an async function.
If you know how to create a pdf file, please let me know.
pdfButton = () => {
const page1 = PDFPage
.create()
.setMediaBox(200, 200)
.drawText('You can add text and rectangles to the PDF!' , {
x: 5,
y: 235,
color : '#007386',
})
.drawRectangle({
x: 25,
y: 25,
width: 150,
height: 150,
color: '#FF99CC',
})
.drawRectangle({
x: 75,
y: 75,
width: 50,
height: 50,
color: '#99FFCC',
});
//It's like a problem.
// const docsDir = await PDFLib.getDocumentsDirectory();
const pdfPath = './data/ex.pdf'; //path to create pdf folder
PDFDocument
.create(pdfPath)
.addPages(page1)
.write() // Returns a promise that resolves with the PDF's path
.then(path => {
console.log('PDF created at: ' + path);
// Do stuff with your shiny new PDF!
});
The pdfButton function must be an async function
pdfButton = async () => {
...
const docsDir = await PDFLib.getDocumentsDirectory();
const pdfPath = `${docsDir}/ex.pdf`;
}

How to convert Assets Images & Icons to PdfImage in flutter using dart_pdf

Used Library: dart_pdf After searching I found the same issue in GITHUB but unable to resolve the issue. I tried this but blurry image appears. Please help!!
ByteData data = await rootBundle.load('assets/test.jpg');
var codec = await instantiateImageCodec(data.buffer.asUint8List());
var frame = await codec.getNextFrame();
var imageBytes = await frame.image.toByteData();
PdfImage assetImage = PdfImage(pdf.document,
image: imageBytes.buffer.asUint8List(), width: 86, height: 80);
Rendered Image:
Use this instead:
final PdfImage assetImage = await pdfImageFromImageProvider(
pdf: pdf.document,
image: const AssetImage('assets/test.jpg'),
);
This function will create your pdf with image and custom data
var pdf = new pw.Document();
Future<pw.Document> createPDF() async {
var assetImage = pw.MemoryImage(
(await rootBundle.load('assets/images/delivery.png'))
.buffer
.asUint8List(),
);
pdf.addPage(pw.Page(
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) {
var width = MediaQuery.of(this.context).size.width;
var height = MediaQuery.of(this.context).size.height;
return pw.Container(
margin: pw.EdgeInsets.only(top: height * 0.1),
child: pw.ListView(
children: [
// your image here
pw.Container(
height: height * 0.25, child: pw.Image(assetImage)),
// other contents
pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.spaceAround,
children: [
pw.Text("order Id:"),
pw.Text(widget.doc['orderId']),
],
),
],
),
);
}));
return pdf;
}
use this function to save
Future savePdf(pw.Document pdfnew) async {
String pdfName;
File file;
try {
var documentDirectory = await AndroidPathProvider.downloadsPath; // for android downloads folder
// var localDirectory = await getApplicationDocumentsDirectory(); // for local directory
setState(() {
pdfName = "your_pdf_name";
});
file = File("$documentDirectory/$pdfName.pdf");
await file.writeAsBytes(await pdf.save());
return file.path;
} catch (e) {
print(e);
}
}

Why just don't execute evaluate in CasperJS?

There is a problem with CasperJS. Everything works until the moment I want to fill a page of text. Write all code:
phantom.casperPath = 'path\\to\\casperjs';
phantom.injectJs(phantom.casperPath + '\\bin\\bootstrap.js');
var url = "someurl";
var someText = "text";
var casper = require('casper').create({
clientScripts: ['jquery.min.js']
});
casper.start(url, function(){
this.echo(this.getCurrentUrl());
this.wait(3000, function() {
this.click('div.but');
});
});
casper.then(function(){
this.evaluate(function(){ //сюда не заходит
document.querySelector("div.anotherElem").innerHTML = someText;
});
});
casper.then(function(){
this.capture('screen.png', {
top: 0,
left: 0,
width: 1280,
height: 1024
});
});
The function evaluate just does not execute. Use the latest version CasperJS, which integrates into PhantomJS (v1.8). Who used casper, tell me what I'm doing wrong.

famous: scrollview within scrollview

I'm trying to create a layout (using famous.js) similar to the BBC News native app; a vertical ScrollView, within which are numerous horizontal ScrollViews. I've got both 'working' to the extent that everything renders and the horizontal scrollers work perfectly. My issue is that the vertical scroll event won't fire if the user swipes a surface within a horizontal ScrollView. If i touch an area outside the horizontal scrollers, the vertical scroller will do its job and scroll... vertically
Does anyone know what i'm missing? I have a feeling that a RenderNode may be needed, but have had no luck so far. I'm just getting to grips with Famous. What i've seen so far is amazing, but not being able to figure this out is really getting to me...
Uber thanks in advance if anyone can help...
/*globals define*/
define(function(require, exports, module) {
// import dependencies
var Modifier = require('famous/core/Modifier');
var Engine = require('famous/core/Engine');
var Surface = require('famous/core/Surface');
var HeaderFooterLayout = require('famous/views/HeaderFooterLayout');
var Scrollview = require('famous/views/Scrollview');
var ContainerSurface = require('famous/surfaces/ContainerSurface');
var mainContext = Engine.createContext();
var layout = new HeaderFooterLayout({
headerSize: 50,
footerSize: 50
});
// create app header and add to layout
var appHeader = new ContainerSurface({
size: [undefined, 50],
classes: ['app-header']
});
appHeader.add(new Surface({
size: [undefined, 50],
content: 'Site Name',
classes: ['app-header__title'],
properties: {
lineHeight: '50px',
textAlign: 'center'
}
}));
layout.header.add(appHeader);
// create page container surface
var page = new ContainerSurface({
properties: {
top: '0'
}
});
// returns a horizontal ScrollView containing
function createCategory() {
var categoryScroll = new Scrollview({
direction: 0,
});
var surfaces = [];
categoryScroll.sequenceFrom(surfaces);
for (var i = 0, temp; i < 7; i++) {
var temp = new Surface({
size: [128, 128],
content: 'surface' + (i + 1),
properties: {
backgroundColor: '#fff',
borderColor: '#303030',
borderStyle: 'solid',
borderWidth: '0px',
borderRightWidth: '4px',
borderLeftWidth: '4px'
}
});
temp.pipe(categoryScroll);
surfaces.push(temp);
}
return categoryScroll;
}
// returns a vertical page scroll
function createPageScroll() {
// create array of horizontal ScrollViews
categories = [];
for (var i = 0; i < 7; i++) {
categories.push(createCategory());
};
var pageScroll = new Scrollview();
var surfaces = [];
for (var i = 0; i < 7; i++) {
temp = new ContainerSurface({
size: [window.innerWidth, 136],
});
temp.add(categories[i]);
surfaces.push(temp);
pageScroll.sequenceFrom(surfaces);
temp.pipe(pageScroll);
};
return pageScroll;
}
layout.content.add(createPageScroll());
mainContext.add(layout);
});
I see you figured it out, but I thought I would post a clean working example for anyone that needed a starting point.. So here it is..
To answer the question, Yes, you needed to pipe your events from the surfaces to each of the scrollviews
var Engine = require("famous/core/Engine");
var Surface = require("famous/core/Surface");
var View = require("famous/core/View");
var Scrollview = require("famous/views/Scrollview");
var ContainerSurface = require("famous/surfaces/ContainerSurface");
var context = Engine.createContext();
var surfaces1 = [];
var surfaces2 = [];
var scrollers = [];
scroll_v_cont = new ContainerSurface({
size:[300,300],
properties: { overflow: 'hidden' }
});
var scroll_v = new Scrollview({ direction: 1 });
scroll_v.sequenceFrom(scrollers);
scroll_v_cont.add(scroll_v);
var scroll_h1_cont = new ContainerSurface({
size:[300,300],
properties: {overflow: 'hidden'}
});
var scroll_h1 = new Scrollview({ direction: 0});
scroll_h1.sequenceFrom(surfaces1);
scroll_h1_cont.add(scroll_h1);
var scroll_h2_cont = new ContainerSurface({
size:[300,300],
properties: { overflow: 'hidden'}
})
var scroll_h2 = new Scrollview({ direction: 0})
scroll_h2.sequenceFrom(surfaces2);
scroll_h2_cont.add(scroll_h2);
scrollers.push(scroll_h1_cont);
scrollers.push(scroll_h2_cont);
for (var i = 0; i < 4; i++) {
var surface1 = new Surface({
content: "Surface: " + (i + 1),
size: [300, 300],
properties: {
backgroundColor: "hsl(" + (i * 360 / 8) + ", 100%, 50%)",
lineHeight: "200px",
textAlign: "center"
}
});
surface1.pipe(scroll_v);
surface1.pipe(scroll_h1);
surfaces1.push(surface1);
var surface2 = new Surface({
content: "Surface: " + (i + 1),
size: [300, 300],
properties: {
backgroundColor: "hsl(" + (i * 360 / 8 + (360 / 8)*4) + ", 100%, 50%)",
lineHeight: "200px",
textAlign: "center"
}
});
surface2.pipe(scroll_v);
surface2.pipe(scroll_h2);
surfaces2.push(surface2);
};
context.add(scroll_v_cont);
the famous-flex FlexScrollView supports vertical & horizontal scrolling restrictions when embedding one scrollview in another. It is described in more detail at the bottom of the FlexScrollView tutorial:
https://github.com/IjzerenHein/famous-flex/blob/master/tutorials/FlexScrollView.md

Tab App fluid width not working

I have a tab app set to fluid on both height and width. Height is no problem ... it is the width that is not working so a good portion of my page (which is only 648px wide) is not showing up. We intentionally set the width to be less than the max so we wouldn't have this problem. To view you can go to the Arrow Fasteners page ... contest or like us to enter tabs.
Tab Apps are only working with a fixed width: 520px. Only height is settable.
Fluid Width is only for Canvas Apps (apps.facebook.com/app-name), that's why these settings are prefixed with "Canvas" (Canvas Width, Canvas Height).
Mike
Add your AppID and the following codes, above the closing body tag. It will work perfectly on tabs.
<script type="text/javascript">
fb_root = document.createElement( 'div' );
fb_root.id = 'fb-root';
try
{
window.document.childNodes[0].appendChild( fb_root );
}
catch( error )
{
window.document.body.appendChild( fb_root );
}
window.fbAsyncInit = function() {
FB.init({
appId : '222222222222222', //your app ID
status : true, // check login status
cookie : true // enable cookies to allow the server to access the session
});
//FB.Canvas.setSize({ width: 520, height: 1400 });
function getDocHeight() {
var D = document;
return Math.max(
Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
Math.max(D.body.clientHeight, D.documentElement.clientHeight)
);
}
var mySetSize = function() {
var height = getDocHeight();
FB.Canvas.setSize({ width: 520, height: height });
setTimeout( mySetSize, 200 );
};
setTimeout( mySetSize, 200 );
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
var head = document.getElementsByTagName('head')[0],
style = document.createElement('style'),
rules = document.createTextNode('body { position: relative; max-width: 520px!important;width: 520px!important; overflow: hidden!important; margin: 0px!important; }');
style.type = 'text/css';
if(style.styleSheet)
style.styleSheet.cssText = rules.nodeValue;
else style.appendChild(rules);
head.appendChild(style);
</script>