svg2pdf

在项目中遇到了svg转pdf,此处记录一下,方便以后使用。

此处使用直接下载的方式,存在自己电脑默认下载目录。

1、maven依赖:

<dependency>

 <groupId>com.itextpdf</groupId>

 <artifactId>itextpdf</artifactId>

 <version>5.5.8</version>

</dependency>

<dependency>

 <groupId>com.itextpdf</groupId>

 <artifactId>itext-pdfa</artifactId>

 <version>5.5.8</version>

</dependency>

<dependency>

 <groupId>com.itextpdf</groupId>

 <artifactId>itext-xtra</artifactId>

 <version>5.5.8</version>

</dependency>

<dependency>

 <groupId>com.itextpdf.tool</groupId>

 <artifactId>xmlworker</artifactId>

 <version>5.5.8</version>

</dependency>

<dependency>  

   <groupId>com.lowagie</groupId>  

   <artifactId>itext</artifactId>  

   <version>2.1.7</version>  

</dependency>  

<dependency>

   <groupId>com.kenai.nbpwr</groupId>

   <artifactId>org-apache-batik-all</artifactId>

   <version>1.7-201003011305</version>

</dependency>

<dependency>

   <groupId>org.eclipse.birt.runtime</groupId>

   <artifactId>org.w3c.dom.smil</artifactId>

   <version>1.0.1.v200903091627</version>

</dependency>

<dependency>

   <groupId>org.eclipse.birt.runtime.3_7_1</groupId>

   <artifactId>org.w3c.dom.svg</artifactId>

   <version>1.1.0</version>

</dependency>

2、html代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>

<script type="text/javascript">

$(function(){

$("#svgToPDF").click(function(){

var svgHtml = $("#svgDiv").find("svg")[0];

var XMLS = new XMLSerializer(); 

var svg = XMLS.serializeToString(svgHtml);

$("#svgHtml").val(svg);

$("#svgForm").submit();

});

});

</script>

</head>

<body>

<div id="svgDiv">

<svg version="1.1" style="font-family:&quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, Arial, Helvetica, sans-serif;font-size:12px;" xmlns="http://www.w3.org/2000/svg" width="800" height="920">

<desc>Created with Highcharts 4.2.5</desc>

<defs>

<clipPath id="highcharts-7"><rect x="0" y="0" width="803" height="483"></rect></clipPath>

</defs>

<rect x="0" y="0" width="800" height="920" fill="#FFFFFF" class=" highcharts-background"></rect>

<g class="highcharts-grid"></g>

<g class="highcharts-grid">

<path fill="none" d="M 144.5 53 L 144.5 856" stroke="#D8D8D8" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 265.5 53 L 265.5 856" stroke="#D8D8D8" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 386.5 53 L 386.5 856" stroke="#D8D8D8" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 506.5 53 L 506.5 856" stroke="#D8D8D8" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 628.5 53 L 628.5 856" stroke="#D8D8D8" stroke-width="1" opacity="1"></path>

</g>

<g class="highcharts-axis">

<path fill="none" d="M 145 88.5 L 135 88.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 123.5 L 135 123.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 158.5 L 135 158.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 193.5 L 135 193.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 228.5 L 135 228.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 262.5 L 135 262.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 297.5 L 135 297.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 332.5 L 135 332.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 367.5 L 135 367.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 402.5 L 135 402.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 437.5 L 135 437.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 472.5 L 135 472.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 507.5 L 135 507.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 542.5 L 135 542.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 577.5 L 135 577.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 612.5 L 135 612.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 647.5 L 135 647.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 681.5 L 135 681.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 716.5 L 135 716.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 751.5 L 135 751.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 786.5 L 135 786.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 821.5 L 135 821.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 856.5 L 135 856.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 145 52.5 L 135 52.5" stroke="#C0D0E0" stroke-width="1" opacity="1"></path>

<path fill="none" d="M 144.5 53 L 144.5 856" stroke="#C0D0E0" stroke-width="1"></path>

</g>

<g class="highcharts-axis">

<text x="628" text-anchor="end" transform="translate(0,0)" class=" highcharts-yaxis-title" style="color:#707070;fill:#707070;" y="899">Rating</text>

<path fill="none" d="M 145 856.5 L 628 856.5" stroke="#C0D0E0" stroke-width="1"></path>

</g>

<g class="highcharts-series-group">

<g class="highcharts-series highcharts-series-0 highcharts-tracker" transform="translate(628,856) rotate(90) scale(-1,1) scale(1 1)" style="" width="483" height="803" clip-path="url(#highcharts-7)">

<rect x="777.5" y="241.5" width="15" height="241.5" stroke="#FFFFFF" stroke-width="0" fill="yellow" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="742.5" y="0" width="15" height="483" stroke="#FFFFFF" stroke-width="0" fill="red" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="707.5" y="0" width="15" height="483" stroke="#FFFFFF" stroke-width="0" fill="red" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="672.5" y="241.5" width="15" height="241.5" stroke="#FFFFFF" stroke-width="0" fill="yellow" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="637.5" y="362.25" width="15" height="120.75" stroke="#FFFFFF" stroke-width="0" fill="green" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="602.5" y="120.75" width="15" height="362.25" stroke="#FFFFFF" stroke-width="0" fill="orange" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="568.5" y="241.5" width="15" height="241.5" stroke="#FFFFFF" stroke-width="0" fill="yellow" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="533.5" y="120.75" width="15" height="362.25" stroke="#FFFFFF" stroke-width="0" fill="orange" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="498.5" y="120.75" width="15" height="362.25" stroke="#FFFFFF" stroke-width="0" fill="orange" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="463.5" y="241.5" width="15" height="241.5" stroke="#FFFFFF" stroke-width="0" fill="yellow" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="428.5" y="0" width="15" height="483" stroke="#FFFFFF" stroke-width="0" fill="red" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="393.5" y="241.5" width="15" height="241.5" stroke="#FFFFFF" stroke-width="0" fill="yellow" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="358.5" y="241.5" width="15" height="241.5" stroke="#FFFFFF" stroke-width="1" fill="yellow" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="323.5" y="120.75" width="15" height="362.25" stroke="#FFFFFF" stroke-width="0" fill="orange" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="288.5" y="120.75" width="15" height="362.25" stroke="#FFFFFF" stroke-width="0" fill="orange" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="253.5" y="362.25" width="15" height="120.75" stroke="#FFFFFF" stroke-width="0" fill="green" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="218.5" y="0" width="15" height="483" stroke="#FFFFFF" stroke-width="0" fill="red" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="184.5" y="241.5" width="15" height="241.5" stroke="#FFFFFF" stroke-width="0" fill="yellow" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="149.5" y="241.5" width="15" height="241.5" stroke="#FFFFFF" stroke-width="0" fill="yellow" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="114.5" y="120.75" width="15" height="362.25" stroke="#FFFFFF" stroke-width="0" fill="orange" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="79.5" y="241.5" width="15" height="241.5" stroke="#FFFFFF" stroke-width="0" fill="yellow" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="44.5" y="241.5" width="15" height="241.5" stroke="#FFFFFF" stroke-width="0" fill="yellow" rx="0" ry="0" style="width: 20px;"></rect>

<rect x="9.5" y="362.25" width="15" height="120.75" stroke="#FFFFFF" stroke-width="0" fill="green" rx="0" ry="0" style="width: 20px;"></rect>

</g>

<g class="highcharts-markers highcharts-series-0" transform="translate(628,856) rotate(90) scale(-1,1) scale(1 1)" width="483" height="803" clip-path="none"></g>

</g>

<text x="400" text-anchor="middle" class="highcharts-title" style="color:#333333;font-size:18px;fill:#333333;width:736px;" y="24">

<tspan>Traumatic Experiences</tspan>

</text>

<g class="highcharts-data-labels highcharts-series-0 highcharts-tracker" visibility="visible" transform="translate(145,53) scale(1 1)" opacity="1" style="">

<g style="" transform="translate(242,4)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>1</tspan>

</text>

</g>

<g style="" transform="translate(465,39)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>3</tspan>

</text>

</g>

<g style="" transform="translate(465,74)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>3</tspan>

</text>

</g>

<g style="" transform="translate(242,109)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>1</tspan>

</text>

</g>

<g style="" transform="translate(121,144)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>0</tspan>

</text>

</g>

<g style="" transform="translate(362,179)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>2</tspan>

</text>

</g>

<g style="" transform="translate(242,213)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>1</tspan>

</text>

</g>

<g style="" transform="translate(362,248)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>2</tspan>

</text>

</g>

<g style="" transform="translate(362,283)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>2</tspan>

</text>

</g>

<g style="" transform="translate(242,318)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>1</tspan>

</text>

</g>

<g style="" transform="translate(465,353)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"><tspan>3</tspan></text></g><g style="" transform="translate(242,388)"><text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"><tspan>1</tspan></text></g><g style="" transform="translate(242,423)"><text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>1</tspan>

</text>

</g>

<g style="" transform="translate(362,458)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>2</tspan>

</text>

</g>

<g style="" transform="translate(362,493)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>2</tspan>

</text>

</g>

<g style="" transform="translate(121,528)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>0</tspan>

</text>

</g>

<g style="" transform="translate(465,563)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>3</tspan>

</text>

</g>

<g style="" transform="translate(242,597)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>1</tspan>

</text>

</g>

<g style="" transform="translate(242,632)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>1</tspan>

</text>

</g>

<g style="" transform="translate(362,667)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>2</tspan>

</text>

</g>

<g style="" transform="translate(242,702)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>1</tspan>

</text>

</g>

<g style="" transform="translate(242,737)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>1</tspan>

</text>

</g>

<g style="" transform="translate(121,772)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16">

<tspan>0</tspan>

</text>

</g>

<g style="" transform="translate(0,13)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,48)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,83)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,118)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,153)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,188)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,222)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,257)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,292)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,327)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,362)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,397)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,432)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,467)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,502)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,537)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,572)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,606)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,641)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,676)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,711)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,746)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

<g style="" transform="translate(0,781)">

<text x="5" style="font-size:11px;font-weight:bold;color:#000000;text-shadow:0 0 6px #FFFFFF, 0 0 3px #FFFFFF;fill:#000000;text-rendering:geometricPrecision;" y="16"></text>

</g>

</g>

<g class="highcharts-legend" transform="translate(640,90)">

<rect x="0.5" y="0.5" width="108" height="28" stroke="black" stroke-width="5" fill="none" isShadow="true" stroke-opacity="0.049999999999999996" transform="translate(1, 1)" visibility="visible"></rect>

<rect x="0.5" y="0.5" width="108" height="28" stroke="black" stroke-width="3" fill="none" isShadow="true" stroke-opacity="0.09999999999999999" transform="translate(1, 1)" visibility="visible"></rect>

<rect x="0.5" y="0.5" width="108" height="28" stroke="black" stroke-width="1" fill="none" isShadow="true" stroke-opacity="0.15" transform="translate(1, 1)" visibility="visible"></rect>

<rect x="0.5" y="0.5" width="108" height="28" stroke="#909090" stroke-width="1" fill="#FFFFFF" visibility="visible"></rect>

<g>

<g>

<g class="highcharts-legend-item" transform="translate(8,3)">

<text x="21" style="color:#333333;font-size:12px;font-weight:bold;cursor:pointer;fill:#333333;" text-anchor="start" y="15">

<tspan>gdfsg gfdgs</tspan>

</text>

<rect x="0" y="4" width="16" height="12" fill="#7cb5ec"></rect>

</g>

</g>

</g>

</g>

<g class="highcharts-axis-labels highcharts-xaxis-labels">

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="72" opacity="1">

<tspan style="width:125px; overflow:hidden">Neglect</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="107" opacity="1">

<tspan style="width:125px; overflow:hidden">Sexual Abuse</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="142" opacity="1">

<tspan style="width:125px; overflow:hidden">Physical Abuse</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="177" opacity="1">

<tspan style="width:125px; overflow:hidden">Emotional Abuse</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="212" opacity="1">

<tspan style="width:125px; overflow:hidden">Medical Trauma</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="247" opacity="1">

<tspan style="width:125px; overflow:hidden">Family Violence</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="282" opacity="1">

<tspan style="width:125px; overflow:hidden">Community Violence</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="317" opacity="1">

<tspan style="width:125px; overflow:hidden">School Violence</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="352" opacity="1">

<tspan style="width:125px; overflow:hidden">Disasters</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="387" opacity="1">

<tspan style="width:125px; overflow:hidden">War-Terrorism</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="422" opacity="1">

<tspan style="width:125px; overflow:hidden">Witness Crime</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="457" opacity="1">

<tspan style="width:125px; overflow:hidden">Parental Crime</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="491" opacity="1">

<tspan style="width:125px; overflow:hidden">Disruptions in CG</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="526" opacity="1">

<tspan style="width:125px; overflow:hidden">Exploitation</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="561" opacity="1">

<tspan style="width:125px; overflow:hidden">Accident</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="596" opacity="1">

<tspan style="width:125px; overflow:hidden">Adj to Trauma</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="631" opacity="1">

<tspan style="width:125px; overflow:hidden">*Traumatic Grief</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="666" opacity="1">

<tspan style="width:125px; overflow:hidden">*Re-experiencing</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="701" opacity="1">

<tspan style="width:125px; overflow:hidden">*Hyperarousal</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="736" opacity="1">

<tspan style="width:125px; overflow:hidden">*Avoidance</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="771" opacity="1">

<tspan style="width:125px; overflow:hidden">*Numbing</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="806" opacity="1">

<tspan style="width:125px; overflow:hidden">*Dissociation</tspan>

</text>

<text x="130" style="color:#606060;cursor:default;font-size:11px;width:254px;fill:#606060;text-overflow:clip;" text-anchor="end" transform="translate(0,0)" y="841" opacity="1">

<tspan style="width:125px; overflow:hidden">Emotional Dysreg</tspan>

</text>

</g>

<g class="highcharts-axis-labels highcharts-yaxis-labels">

<text x="145" style="color:#606060;cursor:default;font-size:11px;fill:#606060;width:111px;text-overflow:clip;" text-anchor="middle" transform="translate(0,0)" y="875" opacity="1"></text>

<text x="265.75" style="color:#606060;cursor:default;font-size:11px;fill:#606060;width:111px;text-overflow:clip;" text-anchor="middle" transform="translate(0,0)" y="875" opacity="1">0</text>

<text x="386.5" style="color:#606060;cursor:default;font-size:11px;fill:#606060;width:111px;text-overflow:clip;" text-anchor="middle" transform="translate(0,0)" y="875" opacity="1">1</text>

<text x="507.25" style="color:#606060;cursor:default;font-size:11px;fill:#606060;width:111px;text-overflow:clip;" text-anchor="middle" transform="translate(0,0)" y="875" opacity="1">2</text>

<text x="628" style="color:#606060;cursor:default;font-size:11px;fill:#606060;width:111px;text-overflow:clip;" text-anchor="middle" transform="translate(0,0)" y="875" opacity="1">3</text>

</g>

<g class="highcharts-tooltip" style="cursor:default;padding:0;pointer-events:none;white-space:nowrap;" transform="translate(160,-9999)" opacity="0" visibility="visible">

<path fill="none" d="M 3.5 0.5 L 413.5 0.5 C 416.5 0.5 416.5 0.5 416.5 3.5 L 416.5 85.5 C 416.5 88.5 416.5 88.5 413.5 88.5 L 111.5 88.5 105.5 94.5 99.5 88.5 3.5 88.5 C 0.5 88.5 0.5 88.5 0.5 85.5 L 0.5 3.5 C 0.5 0.5 0.5 0.5 3.5 0.5" isShadow="true" stroke="black" stroke-opacity="0.049999999999999996" stroke-width="5" transform="translate(1, 1)"></path>

<path fill="none" d="M 3.5 0.5 L 413.5 0.5 C 416.5 0.5 416.5 0.5 416.5 3.5 L 416.5 85.5 C 416.5 88.5 416.5 88.5 413.5 88.5 L 111.5 88.5 105.5 94.5 99.5 88.5 3.5 88.5 C 0.5 88.5 0.5 88.5 0.5 85.5 L 0.5 3.5 C 0.5 0.5 0.5 0.5 3.5 0.5" isShadow="true" stroke="black" stroke-opacity="0.09999999999999999" stroke-width="3" transform="translate(1, 1)"></path>

<path fill="none" d="M 3.5 0.5 L 413.5 0.5 C 416.5 0.5 416.5 0.5 416.5 3.5 L 416.5 85.5 C 416.5 88.5 416.5 88.5 413.5 88.5 L 111.5 88.5 105.5 94.5 99.5 88.5 3.5 88.5 C 0.5 88.5 0.5 88.5 0.5 85.5 L 0.5 3.5 C 0.5 0.5 0.5 0.5 3.5 0.5" isShadow="true" stroke="black" stroke-opacity="0.15" stroke-width="1" transform="translate(1, 1)"></path>

<path fill="white" d="M 3.5 0.5 L 413.5 0.5 C 416.5 0.5 416.5 0.5 416.5 3.5 L 416.5 85.5 C 416.5 88.5 416.5 88.5 413.5 88.5 L 111.5 88.5 105.5 94.5 99.5 88.5 3.5 88.5 C 0.5 88.5 0.5 88.5 0.5 85.5 L 0.5 3.5 C 0.5 0.5 0.5 0.5 3.5 0.5" stroke="#7cb5ec" stroke-width="1"></path>

</g>

</svg>

</div>

<div>

<div id="formDiv">

<form action="exportPDF.do" id="svgForm" method="post">

<input type="hidden" name="svg" id="svgHtml">

</form>

</div>

<button id="svgToPDF">SVG TO PDF</button>

</div>

</body>

</html>

3、control层:

@RequestMapping(value="exportPDF")

@ResponseBody

public String exportPdfFile(HttpServletRequest request,HttpServletResponse response,String svg) throws Exception {

Calendar cal = Calendar.getInstance();

String pdfName = String.valueOf(cal.getTimeInMillis());

response.reset();

response.addHeader("Content-Type", "application/pdf");

response.addHeader("Content-Disposition", "attachment; filename="+ pdfName + ".pdf");

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

PDF.testPDf(baos,svg);

baos.writeTo(response.getOutputStream());

baos.close();

return "success";

}

4、PDF类:

import java.awt.Graphics2D;

import java.awt.print.PageFormat;

import java.awt.print.Paper;

import java.io.ByteArrayOutputStream;

import java.io.FileOutputStream;

import java.io.StringReader;

import org.apache.batik.transcoder.TranscoderInput;

import org.apache.batik.transcoder.print.PrintTranscoder;

import com.itextpdf.text.Document;

import com.itextpdf.text.Element;

import com.itextpdf.text.ImgTemplate;

import com.itextpdf.text.PageSize;

import com.itextpdf.text.Paragraph;

import com.itextpdf.text.pdf.PdfContentByte;

import com.itextpdf.text.pdf.PdfTemplate;

import com.itextpdf.text.pdf.PdfWriter;

import com.itextpdf.tool.xml.ElementList;

import com.itextpdf.tool.xml.XMLWorkerHelper;

public class PDF {

@SuppressWarnings("deprecation")

public static void testPDf() throws Exception{

Document document = new Document();

        PdfWriter writer = PdfWriter.getInstance(document,

                new FileOutputStream("d:/svg.pdf"));

        document.open();

        document.setPageSize(PageSize.A4);

      

        

        

        

        StringBuffer html = new StringBuffer();       

        html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");       

        html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">").     

             append("<head>")       

            .append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />")     

            .append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: SimSun;}</style>")      

            .append("</head>")       

            .append("<body>");       

        

        html.append("<div>");

        

        

        

        html.append("<div>TESTTTTT</div>"); 

        

        html.append("<div>");

        html.append("<div>");

        html.append("this is test div");

        html.append("</div>");

        html.append("</div>");

        

        

        

        html.append("<table>");

        html.append("<tr>");

        html.append("<td>");

        html.append("this is test table1");

        html.append("</td>");

        html.append("</tr>");

        html.append("</table>");

        

        html.append("<div style=\"border:dotted thin;height: auto; width:400px\" align=\"left\" >");

        html.append("<table class=\"split\" style=\"height: 30px;width: 100px\">");

        html.append("<tr>");

        html.append("<td>");

        html.append("this is test table2");

        html.append("</td>");

        html.append("<td>");

        html.append("this is test table2");

        html.append("</td>");

        html.append("</tr>");

        html.append("</table>");

        html.append("</div>");

        

        

        html.append("<div style=\"color: #0000ff;font-size: 12px;\" >UUUGGGHHFF</div>");       

        html.append("<h1>UUUGGGHHFF</h1>"); 

        

        html.append("</div>");

        html.append("</body></html>");

        Paragraph context = new Paragraph();

        String htmlString = html.toString();

        ElementList elementList =XMLWorkerHelper.parseToElementList(htmlString, null);

        for (Element element : elementList) {

        context.add(element);

        }

        document.add(context);

        //document.add(new Paragraph(html.toString()));

        int width = 500;

        int height = 250;

        PdfContentByte cb = writer.getDirectContent();

        PdfTemplate template = cb.createTemplate(width,height);         

        Graphics2D g2 = template.createGraphics(width,height);          

        

        PrintTranscoder prm = new PrintTranscoder();

        TranscoderInput ti = new TranscoderInput("file:///d:/testxml.xml");

        prm.transcode(ti, null);

       

        PageFormat pg = new PageFormat();

        Paper pp= new Paper();

        pp.setSize(width, height);

        pp.setImageableArea(0, 0, width, height);

        pg.setPaper(pp);

        prm.print(g2, pg, 0); 

        g2.dispose(); 

        ImgTemplate img = new ImgTemplate(template);           

        document.add(img);

        

        document.close();

        System.out.println("转换成功");

}

@SuppressWarnings("deprecation")

public static void testPDf(ByteArrayOutputStream baos,String svg) throws Exception{

Document document = new Document();

        PdfWriter writer = PdfWriter.getInstance(document,baos);

        document.open();

        document.setPageSize(PageSize.A4);

      

        

        

        

        StringBuffer html = new StringBuffer();       

        html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");       

        html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">").     

             append("<head>")       

            .append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />")     

            .append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: SimSun;}</style>")      

            .append("</head>")       

            .append("<body>");       

        html.append("<div>");

        

        

        

        html.append("<div>TESTTTTT</div>"); 

        

        html.append("<div>");

        html.append("<div>");

        html.append("this is test div");

        html.append("</div>");

        html.append("</div>");

        

        

        

        html.append("<table>");

        html.append("<tr>");

        html.append("<td>");

        html.append("this is test table1");

        html.append("</td>");

        html.append("</tr>");

        html.append("</table>");

        

        html.append("<div style=\"border:dotted thin;height: auto; width:400px\" align=\"left\" >");

        html.append("<table class=\"split\" style=\"height: 30px;width: 100px\">");

        html.append("<tr>");

        html.append("<td>");

        html.append("this is test table2");

        html.append("</td>");

        html.append("<td>");

        html.append("this is test table2");

        html.append("</td>");

        html.append("</tr>");

        html.append("</table>");

        html.append("</div>");

        

        

        html.append("<div style=\"color: #0000ff;font-size: 12px;\" >UUUGGGHHFF</div>");       

        html.append("<h1>UUUGGGHHFF</h1>"); 

        

        html.append("</div>");

        html.append("</body></html>");

        Paragraph context = new Paragraph();

        String htmlString = html.toString();

        ElementList elementList =XMLWorkerHelper.parseToElementList(htmlString, null);

        for (Element element : elementList) {

        context.add(element);

        }

        document.add(context);

        //document.add(new Paragraph(html.toString()));

        int width = 500;

        int height = 250;

        PdfContentByte cb = writer.getDirectContent();

        PdfTemplate template = cb.createTemplate(width,height);         

        Graphics2D g2 = template.createGraphics(width,height);          

        

        PrintTranscoder prm = new PrintTranscoder();

        TranscoderInput ti = new TranscoderInput(new StringReader(svg));

        prm.transcode(ti, null);

       

        PageFormat pg = new PageFormat();

        Paper pp= new Paper();

        pp.setSize(width, height);

        pp.setImageableArea(0, 0, width, height);

        pg.setPaper(pp);

        prm.print(g2, pg, 0); 

        g2.dispose(); 

        ImgTemplate img = new ImgTemplate(template);           

        document.add(img);

        

        document.close();

        System.out.println("转换成功");

}

public static void main(String[] args) throws Exception {

PDF.testPDf();

}

}

注意:因为使用的是下载方式,此方式必须使用form表单提交,若使用ajax方式不会下载成功

猜你喜欢

转载自1960370817.iteye.com/blog/2344349
SVG