I'm trying to convert some html like a span element and set its font and size
I need to put this paragraph on top of a text area defined in a pdf form.
This is what i do
//define style
PdfFont fontRegular = FindFontInForm(pdf, new PdfName("OpenSans"));
Style regular = new Style();
regular.SetFont(fontRegular).SetFontSize(9);
//convert html IList<IElement> lst = HtmlConverter.ConvertToElements(val);
Paragraph p = (Paragraph)lst[0]; p.AddStyle(regular);
//size of field, need to put html paragraph on top of it
PdfArray position = toSet.GetWidgets()[0].GetRectangle();
float width = (float)(position.GetAsNumber(2).GetValue() - position.GetAsNumber(0).GetValue());
float height = (float)(position.GetAsNumber(3).GetValue() - position.GetAsNumber(1).GetValue());
Rectangle rect = new Rectangle((float)position.GetAsNumber(0).GetValue(), (float)position.GetAsNumber(1).GetValue(), width, height);
//canvas to add paragraph
Canvas canvasField = new Canvas(canvas, pdf, rect);
canvasField.Add(p);
canvas.Rectangle(rect);
But the font is not applied.
Also Could it be possible to set the font and size in the style of a span html element?
Probabbly it is to late but in any case let me tell you how i fixed this.
Basically you are missing following in order to apply the font
FontProvider provider = new FontProvider();
provider.AddFont([your font]);
ConverterProperties properties = new ConverterProperties();
properties.SetFontProvider(provider);
and then use this:
HtmlConverter.ConvertToElements(stringToConvert, properties);
this way i managed to apply my font to converted html element (paragraph)
I would avoid constructions like this:
PdfFont fontRegular = FindFontInForm(pdf, new PdfName("OpenSans"));
Style regular = new Style();
regular.SetFont(fontRegular).SetFontSize(9);
//convert html IList<IElement> lst = HtmlConverter.ConvertToElements(val);
Paragraph p = (Paragraph)lst[0]; p.AddStyle(regular);
This is counter-intuitive when converting HTML to PDF.
Instead, I would work with CSS to define the font and the size and the style of a span element. With iText 7, you can now even define different MediaQueries. For instance: if you want the HTML to use one specific set of styles when shown in an HTML browser, but you want to use another set of styles when converting the HTML to PDF, you could work with a print.css file.
All of this is explained in the HTML to PDF tutorial.
In chapter 2, you learn how to define styles using CSS. Take a look at this example:
You see that the text "Read more about this movie" has a smaller font-size. and that "IMDB" has a different font color.
That's because the HTML was defined this way:
<div class="imdb">Read more about this movie on
IMDB</div>
And the CSS was defined this way:
.imdb {
font-size: 0.8em;
}
a {
color: green;
}
As you can see, the <div> has a class attribute imdb which is defined in the CSS to have a smaller font size. The CSS for the <a>-tag defines that the text color should be green.
All of this is standard HTML and CSS functionality. There's nothing iText-specific here. Whatever is shown in the browser is also shown on the PDF in this case.
The iText code is as simple as this:
HtmlConverter.convertToPdf(new File(src), new File(dest));
Why would you make things complex if it can be as easy as this?
Chapter 3 explains what to do if you want to create a difference between what's rendered on the screen and what's rendered on the PDF. We use the print.css to achieve this (the PDF will mimic what happens when you print the HTML file).
In your HTML, you might have something like this:
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="css/sxsw.css"/>
<link rel="stylesheet" media="print only" href="css/sxsw_print.css">
</head>
The sxsw.css is the CSS that will be used when showing the HTML in a browser; the print.css will be used when printing the HTML from the browser. Again there's nothing specific to iText in this HTML. This is common functionality known to any HTML developer.
With iText, the sxsw.css will be used if you only use the single line mentioned before. If you want to use the print.css instead, you have to change the ConvertorProperties:
ConverterProperties properties = new ConverterProperties();
properties.setBaseUri(baseUri);
MediaDeviceDescription mediaDeviceDescription =
new MediaDeviceDescription(MediaType.PRINT);
properties.setMediaDeviceDescription(mediaDeviceDescription);
HtmlConverter.convertToPdf(
new FileInputStream(src), new FileOutputStream(dest), properties);
Now, because we have changed the MediaDeviceDescription to MediaType.PRINT, the print.css styles will be used.
The code is only slightly different if you need the iText elements so that you can add them in a specific rectangle. That's explained in chapter 1:
List<IElement> elements =
HtmlConverter.convertToElements(new FileInputStream(src), properties);
You might ask yourself: Why can't I do it my way? Why shouldn't I define the font size, styles, etc... in my code?
The answer is simple: Your code will be hard to maintain! If your employer asks you to apply a change, you will have to change your code, compile it, etc...
If you do it the way it's described in the official tutorial, it's easy: you don't have to change your code; you only have to change the CSS. That's much easier!
Related
currently im trying to style a tooltip which appears when you hover over an map image with dynamic content (title of the company).
My aim is to style the background to a specific color, give the font a color and also apply a CSS property "box-shadow".
For the first aim I tried to use the "fill" property like so:
mapImageSeries is of type am4maps.MapImageSeries.
this.mapImageSeries.tooltip.fill = am4core.color('#ffff00');
Which does not work however using
this.mapImageSeries.tooltip.background.cornerRadius = 0; // will change the "border-radius" of the tooltip.
this.mapImageSeries.tooltip.background.fill = am4core.color('#ffff00'); // does also not work.
For my second goal setting up a color property for the font I didn't find a property, same with the box-shadow css property.
Is it possible to attach a css class for the tooltip so I can easily style it via CSS? And how do I style the tooltip with the
requirements im facing?
By default, tooltips pull colors from their relevant object, so to manipulate their styles you'll first have to turn that off, e.g.:
this.mapImageSeries.tooltip.getFillFromObject = false;
You can then do:
this.mapImageSeries.tooltip.background.cornerRadius = 0;
this.mapImageSeries.tooltip.background.fill = am4core.color("#ffff00");
Instead of modifying CSS box-shadow, you can apply the DropShadow SVG filter. Tooltips have a single filter, actually a DropShadow filter out the box, which we can modify:
var dropShadow = this.mapImageSeries.tooltip.filters.getIndex(0);
dropShadow.dx = 3;
dropShadow.dy = 3;
dropShadow.blur = 5;
dropShadow.opacity = 0.7;
To modify Tooltip text styles, they actually have their own Label child via their label property. There are two ways you can modify color, first is like the method above, e.g. if you want to set a default color for tooltip text:
this.mapImageSeries.tooltip.label.fill = am4core.color("#e97f02"); // color from lolcolors: https://www.webdesignrankings.com/resources/lolcolors/#palette_18
Another way to color the text, as well as apply other CSS styles, is to use Visual formatting in your tooltipText string, e.g.:
this.mapImageSeries.tooltipText = "[font-size: 20px; #bd1550]{companyTitle}:[/]\n{locationTitle} branch";
One style that won't work via visual formatting is text-align, you'll need to do that through via SVG properties, e.g.
this.mapImageSeries.tooltip.label.textAlign = "middle";
I've made a demo for you here:
https://codepen.io/team/amcharts/pen/f6d4167ea7ccd5dd47054d2430443c0a/
Hope this helps, let me know if it's all making sense.
If you're still looking to use literally CSS for your own needs, let me know and I'll try to sort that out with you.
Is it possible to style the Category axis labels as seen in the screenshot? Only basic text is allowed in the settings (no HTML) and using :before or :after selectors doesn't have any effect. Any ideas?
If you're using AmCharts 3, you can set the labelText property of a AmGraph to a string containing tags like [[value]], [[description]], [[percents]], [[open]], [[category]] and HTML tags. For example, you could use something like:
labelText: "<b>[[value]] BILLION</b><br>[[category]]"
To change color of a category, when config amcharts, you need to define some colors in "colors" array.
What else do you need in term of "style" ?
Here's a related image:
I want to achieve something like what's pictured on the right side of my image. But I also have a parent container that has a background image of its own, instead of a solid color.
Any advice?
EDIT: Forgot to add, cross-browser compatibility is important. (Or atleast Firefox).
I can only think of one pure CSS solution and it is simply insane.
Let's say your image has a width of 100px. You'll have to create a div that's 100px wide and give it 100 children that are each 1px wide, that each have the same background (positioned accordingly) and that each have an opacity from 0 (the first child) to .99 (the last child).
Personally, I think it's crazy and I'd never use this method.
Rory O'Kane came with a nice and clean solution and I also have another idea which involves JavaScript.
Basically, the idea is that you use a canvas element (support), draw your image on it, loop through its pixels and adjust the alpha for each.
demo
(scroll down to see the result)
Relevant HTML:
<div class='parent'>
<canvas id='c' width='575' height='431'></canvas>
</div>
Relevant CSS (setting the background image on the parent)
.parent {
background: url(parent-background.jpg);
}
JavaScript:
window.onload = function() {
var c = document.getElementById('c'),
ctxt = c.getContext('2d'),
img = new Image();
img.onload = function() {
ctxt.drawImage(img, 0, 0);
var imageData = ctxt.getImageData(0, 0, 575, 431);
for(var i = 0, n = imageData.data.length; i < n; i += 4) {
imageData.data[i + 3] = 255*((i/4)%575)/575;
}
ctxt.putImageData(imageData, 0, 0);
};
/* images drawn onto the canvas must be hosted on the same web server
with the same domain as the code executing it */
/* or they can be encoded like in the demo */
img.src = 'image-drawn-on-canvas.jpg';
};
check these out maybe helpful
DEMO 1
DEMO 2
Ignoring possible CSS-only methods, you can make the image a PNG with the transparent gradient built in to the image’s alpha channel. All browsers support PNG transparency, except for IE 6 and below. Here’s what your sample image would look like as a PNG with a transparent gradient (try putting this image against other backgrounds):
If the images are user-submitted so you can’t add the gradient ahead of time, you could create and store a gradient-added version of each image at the time that the user uploads them.
CSS only method:
https://gist.github.com/3750808
I'm trying to embed a google map into a landscape PDF, but somehow, wkhtmltopdf always cuts the map in two parts although the map would fit on one page easily.
I think the problem is, that the map is built with tiles. The tiles are bigger than the map and are cut off, but wkhtmltopdf seems to ignore this and thinks that the cut off tiles also must fit onto the page...
Here's some sample code to reproduce this:
<html>
<head>
<script src="https://maps.google.com/maps/api/js?sensor=false"></script>
<script>
window.onload = function(){
var fenway = new google.maps.LatLng(47.188563,8.480487);
var gmap = new google.maps.Map(document.getElementById("map"),{
center: fenway,
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
});
var marker = new google.maps.Marker({
position: fenway,
map:gmap
});
google.maps.event.addListener(gmap,"tilesloaded",function(){
window.status = "ready";
});
}
</script>
</head>
<body>
<div id="map" style="width:1500px;height:800px"></div>
</body>
</html>
And the command to convert it to PDF:
wkhtmltopdf --window-status ready --orientation landscape map.html map.pdf
I'm using the latest version of wkhtmltopdf by the way...
Is there a possibility to make the map fill the page without the cut?
You can use the --page-width and --page-height options to wkhtmltopdf to specify a size with the same aspect ratio as the page. You'll probably still have to combine this with specifying a width and height on the body, but it's useful if you want to completely fill the output PDF with content.
For reference, the 'UnitReal' accepted by the --page-width and --page-height options accept floating point values with units:
(none) == mm
mm
cm == 10mm
m == 1000mm
didot
inch
in == inch
pica
pc == pica
cicero
pixel
px == pixel
point
pt == point
But note that they must both be specified with the same unit.
It's not really disabling page breaks, but setting a body height does render your map on just one page.
<body style="height:1000px;">
<div id="map" style="width:1500px;height:800px;"></div>
</body>
How can I change the color or the transparency of the popup's overlay? I want to have another color and alpha 1.
http://mprami.wordpress.com/2008/04/22/alert_popup_modal_transparancy_color_blur_changes/
In flex ‘’ tag has mainly four attributes related to modal properties of pop-ups.
modalTransparency
modalTransparencyBlur
modalTransparencyColor
modalTransparencyDuration
In spark it looks like these were renamed slightly:
modal-transparency
modal-transparency-color
modal-transparency-duration
modal-transparency-blur (guessing on this one)
To extend on artjumble's answer, if you're using a css file, you can also declare it like that in the css file:
global {
modalTransparencyBlur: 0;
modalTransparency: 0.3;
modalTransparencyColor: black;
modalTransparencyDuration: 500;
}