pdfHTML: Support for linearGradient in CSS and SVG
Introduction
In pdfHTML 3.0.1 (as well as iText Core release 7.1.12) came support for the linearGradient attribute within SVG input as well as with CSS styling. In a related article we demonstrated how to use this functionality within the iText 7 Core layout engine, while here we shows how it works when using pdfHTML.
Below are two quick examples which demonstrate this functionality.
CSS Example:
Link to HTML input for this PDF: linearGradient.html
Link to resultant PDF: CSSOutput.pdf
JAVA
package Example;
import java.io.File;
import java.io.IOException;
import com.itextpdf.html2pdf.HtmlConverter;
public class CSStoPDF {
public static final String DEST = "output_file_path/CSSOutput.pdf";
public static final String SRC = "input_file_path/linearGradient.html";
public static void main(String[] args) throws IOException
{
File file = new File(DEST);
file.getParentFile().mkdirs();
new CSStoPDF().createPdf(SRC, DEST);
}
public void createPdf(String src, String dest) throws IOException
{
HtmlConverter.convertToPdf(new File(src), new File(dest));
}
}
C#
using System;
using System.IO;
using iText.Html2Pdf;
namespace Example
{
public class CSSToPDF
{
public const String DEST = "output_file_path/CSSOutput.pdf";
public const String HTML = "input_file_path/linearGradient.html";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory().Create();
new CSSToPDF().CreatePdf();
}
public virtual void CreatePdf()
{
HtmlConverter.ConvertToPdf(new FileStream(HTML, FileMode.Open), new FileStream(DEST, FileMode.Create));
}
}
}
This code yields the following resultant PDF (screenshot):
SVG Example:
Link to SVG input for this PDF: linearGradient.SVG
Link to resultant PDF: SVGOutput.pdf
JAVA
package Example;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.svg.converter.SvgConverter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class SVGtoPDF {
public static final String DEST = "output_file_path/SVGOutput.pdf";
public static final String SVG = "input_file_path/linearGradient.svg";
public static void main(String[] args) throws IOException
{
File file = new File(DEST);
file.getParentFile().mkdirs();
new SVGtoPDF().createPdf();
}
public void createPdf() throws IOException
{
try (FileInputStream svg = new FileInputStream(SVG))
{
// This approach (commented below) directly converts from SVG to PDF.
//SvgConverter.createPdf(new File(SVG), new File(DEST));
// This approach (below) creates a PDF document, then adds SVG to that document.
PdfDocument pdf = new PdfDocument(new PdfWriter(DEST));
Document doc = new Document(pdf);
Image converted = SvgConverter.convertToImage(svg, pdf);
doc.add(converted);
doc.close();
}
}
}
C#
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Svg.Converter;
namespace Example
{
public class SVGtoPDF
{
public const String DEST = "output_file_path/SVGOutput.pdf";
public const String SVG = "input_file_path/linearGradient.svg";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new SVGtoPDF().CreatePdf(DEST);
}
public virtual void CreatePdf(String dest)
{
using (FileStream svg = File.Open(SVG, FileMode.Open))
{
// This approach (commented below) directly converts from SVG to PDF.
//SvgConverter.CreatePdf(new FileInfo(SVG), new FileInfo(DEST));
// This approach (below) creates a PDF document, then adds SVG to that document.
PdfDocument pdf = new PdfDocument(new PdfWriter(DEST));
Document doc = new Document(pdf);
Image converted = SvgConverter.ConvertToImage(svg, pdf);
doc.Add(converted);
doc.Close();
}
}
}
}
This code yields the following resultant PDF (screenshot):