1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
@Component public class HtmlToPDFUtil {
public static String render(String templateFileName, Object data) throws Exception { Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS); cfg.setClassForTemplateLoading(PdfTemplateUtil.class,"/template"); Template template = cfg.getTemplate(templateFileName, "UTF-8"); StringWriter writer = new StringWriter();
template.process(data, writer); writer.flush(); return writer.toString(); }
public static String getPDFBase64ByHtml(String html) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ITextRenderer renderer = new ITextRenderer(); SharedContext sharedContext = renderer.getSharedContext(); sharedContext.setReplacedElementFactory(new B64ImgReplacedElementFactory()); sharedContext.getTextRenderer().setSmoothingThreshold(0); ITextFontResolver fontResolver = renderer.getFontResolver(); fontResolver.addFont("/template/font/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); renderer.setDocumentFromString(html); renderer.layout(); renderer.createPDF(baos);
return Base64.encodeBytes(baos.toByteArray()); }
public static ByteArrayOutputStream base64ToPDF(String base64) { byte[] bytes = Base64.decode(base64); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { out.write(bytes); out.flush(); } catch (IOException e) { e.printStackTrace(); }
return out; }
public static String pdfAddWaterHeaderLogo(String pdfBase64 , boolean isAddWatermark, String waterMarkName , boolean isAddPageNumbers, String headerText , boolean isAddLogoImg, String logoPath, float newWidth, float newHeight, float absoluteX, float absoluteY , boolean isAddWatermarkLogo, String watermarkLogoPath , boolean isAddBgImage, String bgImagePath ) { try { byte[] bytes = Base64.decode(pdfBase64);
PdfReader reader = new PdfReader(bytes); ByteArrayOutputStream bos = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper(reader, bos);
int total = reader.getNumberOfPages();
if (isAddBgImage) { addBackgroundImage(stamper, reader, bgImagePath); }
if (isAddLogoImg) { addLogoImg(stamper, reader, logoPath, newWidth, newHeight, absoluteX, absoluteY); }
if (isAddPageNumbers) { addPageNumbers(stamper, reader, headerText); }
if (isAddWatermark) { addWatermark(stamper, reader, waterMarkName); }
if (isAddWatermarkLogo) { addWatermarkImage(stamper, reader, watermarkLogoPath); }
stamper.close(); reader.close(); return Base64.encodeBytes(bos.toByteArray()); } catch (IOException | DocumentException e) { e.printStackTrace(); } return null; }
private static void addWatermark(PdfStamper stamper, PdfReader reader, String waterMarkName) throws DocumentException, IOException { BaseFont baseFont = BaseFont.createFont("/template/font/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Rectangle pageRect; PdfGState gs = new PdfGState(); gs.setFillOpacity(0.1f); gs.setStrokeOpacity(0.1f);
JLabel label = new JLabel(); FontMetrics metrics; int textH = 0; int textW = 0; label.setText(waterMarkName); metrics = label.getFontMetrics(label.getFont()); textH = metrics.getHeight(); textW = metrics.stringWidth(label.getText());
PdfContentByte under; for (int i = 1; i <= reader.getNumberOfPages(); i++) { pageRect = reader.getPageSizeWithRotation(i); under = stamper.getOverContent(i); under.saveState(); under.setGState(gs); under.beginText(); under.setFontAndSize(baseFont, 20); under.setColorFill(BaseColor.BLACK);
for (int height = -5 + textH; height < pageRect.getHeight(); height = height + textH * 4) { for (int width = -5 + textW; width < pageRect.getWidth() + textW; width = width + textW * 3) { under.showTextAligned(Element.ALIGN_LEFT, waterMarkName, width - textW, height - textH, 30); } } under.endText(); } }
private static void addPageNumbers(PdfStamper stamper, PdfReader reader, String headerText) throws DocumentException, IOException { int total = reader.getNumberOfPages(); for (int i = 1; i <= total; i++) { Rectangle pageRect = reader.getPageSizeWithRotation(i); PdfContentByte content = stamper.getOverContent(i);
BaseFont baseFont = BaseFont.createFont("/template/font/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); content.setFontAndSize(baseFont, 20);
ColumnText.showTextAligned(content, Element.ALIGN_CENTER, new Phrase(headerText, new Font(baseFont, 10)), pageRect.getWidth() / 2, pageRect.getTop() - 20, 0);
ColumnText.showTextAligned(content, Element.ALIGN_CENTER, new Phrase("页码 " + i + " / " + total, new Font(baseFont, 10)), pageRect.getRight()-50, pageRect.getBottom() + 10, 0);
ColumnText.showTextAligned(content, Element.ALIGN_CENTER, new Phrase("Copyright © 2023 HollowStars. All rights reserved.", new Font(baseFont, 10)), (pageRect.getLeft() + pageRect.getRight()) / 2, pageRect.getBottom() + 10, 0);
} }
private static void addLogoImg(PdfStamper stamper, PdfReader reader, String logoPath, float newWidth, float newHeight, float absoluteX, float absoluteY) throws DocumentException, IOException { for (int i = 1; i <= reader.getNumberOfPages(); i++) { Rectangle pageRect = reader.getPageSizeWithRotation(i); PdfContentByte content = stamper.getOverContent(i);
BaseFont baseFont = BaseFont.createFont("/template/font/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); content.setFontAndSize(baseFont, 20);
Image logoImage = Image.getInstance(logoPath); logoImage.scaleAbsolute(newWidth, newHeight); logoImage.setAbsolutePosition(pageRect.getLeft() + absoluteX, pageRect.getTop() + absoluteY); content.addImage(logoImage);
} }
private static void addWatermarkImage(PdfStamper stamper, PdfReader reader, String watermarkPath) throws DocumentException, IOException { Image watermarkImage = Image.getInstance(watermarkPath); watermarkImage.scaleToFit(400, 400);
for (int i = 1; i <= reader.getNumberOfPages(); i++) { Rectangle pageRect = reader.getPageSizeWithRotation(i); PdfContentByte content = stamper.getOverContent(i);
PdfGState gState = new PdfGState(); gState.setFillOpacity(0.1f);
content.saveState(); content.setGState(gState);
float x = (pageRect.getLeft() + pageRect.getRight() - watermarkImage.getScaledWidth()) / 2; float y = (pageRect.getBottom() + pageRect.getTop() - watermarkImage.getScaledHeight()) / 2; content.addImage(watermarkImage, watermarkImage.getScaledWidth(), 0, 0, watermarkImage.getScaledHeight(), x, y);
content.restoreState(); } }
private static void addBackgroundImage(PdfStamper stamper, PdfReader reader, String bgImagePath) throws DocumentException, IOException { Image backgroundImage = Image.getInstance(bgImagePath); backgroundImage.setAbsolutePosition(0, 0); backgroundImage.scaleAbsolute(595, 842);
for (int i = 1; i <= reader.getNumberOfPages(); i++) { Rectangle pageRect = reader.getPageSizeWithRotation(i); PdfContentByte content = stamper.getOverContent(i);
PdfGState gState = new PdfGState(); gState.setFillOpacity(0.2f);
content.saveState(); content.setGState(gState);
float x = (pageRect.getLeft() + pageRect.getRight() - backgroundImage.getScaledWidth()) / 2; float y = (pageRect.getBottom() + pageRect.getTop() - backgroundImage.getScaledHeight()) / 2; content.addImage(backgroundImage, backgroundImage.getScaledWidth(), 0, 0, backgroundImage.getScaledHeight(), x, y);
content.restoreState(); } }
}
|