PdfPrinter.java 13.4 KB
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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
package fi.insomnia.bortal.beanutil;

import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pdfjet.A4;
import com.pdfjet.CoreFont;
import com.pdfjet.Font;
import com.pdfjet.Line;
import com.pdfjet.PDF;
import com.pdfjet.Page;
import com.pdfjet.TextLine;

import fi.insomnia.bortal.model.Bill;
import fi.insomnia.bortal.model.BillLine;
import fi.insomnia.bortal.model.EventOrganiser;
import fi.insomnia.bortal.model.LanEvent;
import fi.insomnia.bortal.utilities.BillUtils;

public class PdfPrinter {
	public enum LineStyle {
		THIN, THICK, NORMAL

	}

	private static final String fontname = CoreFont.HELVETICA;
	private static final String boldfontname = CoreFont.HELVETICA_BOLD;
	private static final String EURO = " EUR";
	// Legacy y-coordinater reverse constant
	private static final double INVERT = 840;
	private static final BigDecimal DEPERCENTER = new BigDecimal(100);

	// private Biller billerInfo = new Biller();
	// private Receiver receiver = new Receiver();
	private Page page;
	private PDF pdf;
	private final Bill bill;
	private static final Logger logger = LoggerFactory.getLogger(PdfPrinter.class);
	private static final String DATEFORMAT = "dd.MM.yyyy";

	public PdfPrinter(Bill printable) {
		bill = printable;
	}

	public void output(OutputStream ostream) {
		try {
			pdf = new PDF(ostream);

			page = new Page(pdf, A4.PORTRAIT);

			// bill = testLines();
			drawHeader();
			drawProducts(bill.getBillLines());
			pankkisiirto();
			pdf.flush();
			// pdf.wrap();
			// String fileName = "/Users/tuomari/Example_01.pdf";
			// FileOutputStream fos = new FileOutputStream(fileName);
			// pdf.getData().writeTo(fos);
			// fos.close();
			// return pdf.getData();

		} catch (Exception e) {
			logger.warn("Error printing bill " + bill + " to stream", e);

		}
	}

	private void drawProducts(List<BillLine> prods) throws Exception {

		Font pagetext = new Font(pdf, fontname);
		pagetext.setSize(11);

		Font pagetitle = new Font(pdf, boldfontname);
		pagetitle.setSize(11);

		// Ihan ensin tulostetaan selityskentät tuotteille.
		drawText(20, 640, "Tuotteet", pagetitle);
		drawText(210, 640, "Määrä", pagetitle);
		drawText(250, 640, "Yks.", pagetitle);
		drawText(295, 640, "A-hinta", pagetitle);
		drawText(362, 640, "Veroton", pagetitle);
		drawText(450, 640, "Alv, (%)", pagetitle);
		drawText(520, 640, "Yhteensä", pagetitle);

		boolean firstpage = true;

		double height = 625.0;
		for (BillLine line : prods) {
			if (firstpage && height < 625 - (15 * 17)) {
				drawText(250, 800, "Sivu 1/2", pagetext);
				page = new Page(pdf, A4.PORTRAIT);
				drawHeader();
				// $this->pdfheader($pdf, $firstpage, $fontit, $laskuArray,
				// $summaArray,$setupArray);
				// $this->infoKentat($pdf, $firstpage, $setupArray, $fontit,
				// 15);
				drawText(250, 800, "Sivu 2/2", pagetext);
				height = 625;

			}

			// $kerroin = 5;
			// $ykshinta = help::pilkoLuku($tuote['hinta']);
			// $kokhinta =
			// help::pilkoLuku(floatval($tuote['hinta'])*floatval($tuote['lkm']));
			// $alvhinta =
			// help::pilkoLuku(floatval($tuote['hinta'])*floatval($tuote['lkm'])*
			// (floatval($tuote['alv'])/100)) .
			// " (" . help::pilkoluku($tuote['alv']) . ")";
			//
			//
			// $yhthinta = help::pilkoLuku($tuote['hinta'] * $tuote['lkm'] *
			// ($tuote['alv']/100 + 1));

			drawText(20, height, line.getName(), pagetext);
			drawText(239, height, line.getQuantity(), pagetext);

			drawText(252, height, line.getUnitName(), pagetext);

			drawText(332, height, line.getUnitPrice(), pagetext);

			drawText(402, height, line.getLinePriceVatless(), pagetext);
			String vat = decRound(line.getLineVat()) + " (" + DEPERCENTER.multiply(line.getVat()).setScale(0, RoundingMode.HALF_UP) + ")";
			drawText(490 - pagetext.stringWidth(vat), height, vat, pagetext);

			drawText(567, height, line.getLinePrice(), pagetext);

			height -= 15;

		}
	}

	private void drawText(double x, double y, BigDecimal num, Font font) throws Exception {
		String nnum = num.setScale(2, RoundingMode.HALF_UP).toString();

		drawText(x - font.stringWidth(nnum), y, nnum, font);

	}

	private void drawSender(int xpos, int ystart) throws Exception {
		Font sendername = new Font(pdf, boldfontname);
		sendername.setSize(17);

		Font addressfont = new Font(pdf, fontname);
		addressfont.setSize(12);
		// User user = bill.getUser();
		int space = 12;
		LanEvent event = bill.getEvent();
		logger.debug("bill is for event {}", event);
		EventOrganiser org = event.getOrganiser();
		logger.debug("and organiser {}", org);
		drawText(xpos, ystart, org.getBillAddress1(), sendername);
		drawText(xpos, ystart - space, org.getBillAddress2(), addressfont);
		drawText(xpos, ystart - space * 2, org.getBillAddress3(), addressfont);
		drawText(xpos, ystart - space * 3, org.getBillAddress4(), addressfont);

	}

	private void drawHeader() throws Exception {

		Font addressfont = new Font(pdf, fontname);
		addressfont.setSize(12);
		drawSender(35, 800);
		Font receivername = new Font(pdf, boldfontname);
		receivername.setSize(13);

		drawText(70, 730, bill.getAddr1(), receivername);
		drawText(70, 718, bill.getAddr2(), addressfont);
		drawText(70, 706, bill.getAddr3(), addressfont);
		drawText(70, 694, bill.getAddr4(), addressfont);
		drawText(70, 682, bill.getAddr5(), addressfont);

		drawThinLine(10, 660, 585, 660);

		drawThinLine(300, 660, 300, 820);
		drawThinLine(300, 820, 585, 820);

		drawThinLine(440, 760, 440, 820);
		drawThinLine(300, 800, 585, 800);
		drawThinLine(300, 780, 585, 780);
		drawThinLine(300, 760, 585, 760);

		Font descriptionfont = new Font(pdf, fontname);
		descriptionfont.setSize(8);

		Font infofont = new Font(pdf, fontname);
		infofont.setSize(11);

		SimpleDateFormat dateformat = new SimpleDateFormat(DATEFORMAT);
		drawText(302, 812, "Päivämäärä", descriptionfont);
		drawText(355, 805, dateformat.format(bill.getSentDate().getTime()), infofont);

		drawText(442, 812, "Eräpäivä", descriptionfont);

		drawText(485, 805, dateformat.format(bill.getDueDate().getTime()), infofont);

		drawText(302, 772, "Maksuehdot", descriptionfont);
		String paystr = "Heti";
		if (bill.getPaymentTime() > 0) {
			paystr = bill.getPaymentTime() + " pv Netto";
		}
		drawText(355, 765, paystr, infofont);

		drawText(442, 792, "Laskun numero", descriptionfont);
		drawText(510, 785, bill.getBillNumber().toString(), infofont);

		/*
		 * drawText(442, 772, "Huomautusaika", descriptionfont); drawText(510,
		 * 765, bill.getNoticetime(), infofont);
		 */
		drawText(302, 792, "Viitenumero", descriptionfont);
		drawText(355, 785, BillUtils.createReferenceNumber(bill.getReferenceNumberBase()).toString(), infofont);

		drawThinLine(300, 740, 585, 740);
		drawText(302, 732, "Viitteenne", descriptionfont);
		drawText(355, 725, bill.getTheirReference(), infofont);

		drawText(302, 712, "Viitteemme", descriptionfont);
		drawText(355, 705, bill.getOurReference(), infofont);
		drawThinLine(300, 720, 585, 720);

		drawText(302, 752, "Toimitus", descriptionfont);
		drawText(355, 745, bill.getDeliveryTerms(), infofont);
		drawThinLine(300, 700, 585, 700);

		drawThinLine(300, 680, 585, 680);

		drawThinLine(470, 660, 470, 700);

		drawText(472, 692, "ALV", descriptionfont);
		drawText(490, 685, decRound(bill.totalVat()) + EURO, infofont);

		drawText(302, 692, "0% ALV", descriptionfont);
		drawText(355, 685, decRound(bill.totalPriceVatless()) + EURO, infofont);

		drawText(302, 672, "Yhteensä", descriptionfont);
		drawText(355, 665, decRound(bill.totalPrice()) + EURO, infofont);
		/*
		 * drawText(472, 672, "Viivästyskorko", descriptionfont); drawText(530,
		 * 665, bill.getDelayIntrest() + "%", infofont);
		 */
		// drawText( 490, 665, "ALV Rek.",'mediumTeksti');

		// Alkuosa loppuu

	}

	private static String decRound(BigDecimal num) {
		return num.setScale(2, RoundingMode.HALF_UP).toString();
	}

	private void pankkisiirto() throws Exception {
		// Pankkisiirtolappuosuus.

		// Paksu alavasen
		drawLine(new double[] { 10, 317, 317 }, new double[] { 102, 102, 78 });
		// Iso paksu oikealla.
		drawLine(new double[] { 317, 317, 576 }, new double[] { 278, 102, 102 });
		// Paksu maksajan ympärillä vasen ylä
		drawLine(new double[] { 10, 57, 57 }, new double[] { 240, 240, 198 });
		// paksu ylin vasemmalla
		drawLine(new double[] { 57, 57, 317 }, new double[] { 278, 240, 240 });
		// Alimmainen viiva
		drawLine(10, 78, 576, 78);
		// Viitenro:n yl&auml;puolella
		drawLine(317, 126, 576, 126);
		// pysty viite ja eräpäivän erotin
		drawLine(348, 126, 348, 78);
		// eräpäivä ja euroerotin.
		drawLine(442, 102, 442, 78);
		// Saajan ja maksajan v&auml;liss&auml;
		drawLine(10, 198, 317, 198);

		// ohuet

		// Allekirjoitus
		drawThinLine(57, 114, 317, 114);

		// Tilin erotin
		drawThinLine(57.6, 78, 57.6, 102);

		// pienet v&auml;k&auml;set
		for (double i = 72; i < 275; i += 14.4) {
			drawThinLine(new double[] { i, i }, new double[] { 78, 85.2 });
		}
		// Viiva v&auml;k&auml;sten v&auml;liin
		drawLine(149, 84, 154, 84);

		// Tulostetaan katkoviiva Tilisiirron yl&auml;puolelle.
		for (double i = 10; i < 576; i = i + 20) {
			drawThinLine(new double[] { i, i + 10 }, new double[] { 288, 288 });
		}

		// $pdf->draw_text()
		// kirjoitukset
		Font billfont = new Font(pdf, fontname);
		billfont.setSize(7);

		drawText(31, 267, "Saajan", billfont);
		drawText(23, 261, "tilinumero", billfont);
		drawText(14, 255, "Mottagarens", billfont);
		drawText(10, 249, "kontonummer", billfont);

		drawText(35, 220, "Saaja", billfont);
		drawText(22, 214, "Mottagare", billfont);

		drawText(27, 180, "Maksaja", billfont);
		drawText(27, 174, "Betalare", billfont);

		drawText(18, 120, "Allekirjoitus", billfont);
		drawText(19, 114, "Undesrkrift", billfont);

		drawText(25, 91, "Tililtä nro", billfont);
		drawText(12, 85, "Från konto nr", billfont);

		Font titletext = new Font(pdf, boldfontname);
		titletext.setSize(10);

		drawText(320, 267, "TILISIIRTO GIRERING", titletext);

		Font smallPrint = new Font(pdf, fontname);
		smallPrint.setSize(5);

		drawText(320, 260, "Maksu välitetään saajalle vain Suomessa Kotimaan maksujenvälituksen yleisten", smallPrint);
		drawText(320, 255, "ehtojen mukaisesti ja vain maksajan ilmoittaman tilinumeron perusteella.", smallPrint);
		drawText(320, 248, "Betalningen förmedlas fill mottagare endast i Finland enligt Allmänna vllkor för", smallPrint);
		drawText(320, 243, "inrikes betalningsförmedling och endast till det kontonummer betalaren angivit.", smallPrint);

		drawText(319, 116, "Viitenro", billfont);
		drawText(319, 108, "Ref.nr", billfont);
		drawText(319, 92, "Eräpäivä", billfont);
		drawText(319, 84, "Forf.dag", billfont);
		drawText(444, 95, "Euro", billfont);

		Font banktext = new Font(pdf, boldfontname);
		banktext.setSize(8);

		drawText(545, 24, "PANKKI", banktext);
		drawText(542, 15, "BANKEN", banktext);

		// laskun tiedot
		Font addrfont = new Font(pdf, fontname);
		addrfont.setSize(11); // Tulostetaan tekstit pankkisiirtolomakkeeseen.

		Font receivername = new Font(pdf, boldfontname);
		receivername.setSize(12);

		// Laskuttajan tiedot
		EventOrganiser organiser = bill.getEvent().getOrganiser();
		drawText(70, 220, organiser.getBillAddress1(), receivername);
		drawText(70, 265, organiser.getBankNumber1() + " " + organiser.getBankName1(), addrfont);
		drawText(70, 250, organiser.getBankNumber2() + " " + organiser.getBankName2(), addrfont);

		drawText(70, 180, bill.getAddr1(), receivername);
		drawText(70, 168, bill.getAddr2(), addrfont);
		drawText(70, 156, bill.getAddr3(), addrfont);
		drawText(70, 144, bill.getAddr4(), addrfont);
		drawText(70, 132, bill.getAddr5(), addrfont);
		drawText(355, 110, BillUtils.createReferenceNumber(bill.getReferenceNumberBase()).toString(), addrfont);
		drawText(465, 86, decRound(bill.totalPrice()), addrfont);
		drawText(355, 86, new SimpleDateFormat(DATEFORMAT).format(bill.getDueDate().getTime()), addrfont);

	}

	private void drawThinLine(double[] ds, double[] ds2) throws Exception {
		drawLine(ds, ds2, LineStyle.THIN);
	}

	private void drawLine(double[] xs, double[] ys) throws Exception {
		drawLine(xs, ys, LineStyle.NORMAL);
	}

	private void drawLine(double[] ds, double[] ds2, LineStyle style) throws Exception {
		for (int i = 0; i + 1 < ds.length && i + 1 < ds2.length; ++i) {
			drawLine(ds[i], ds2[i], ds[i + 1], ds2[i + 1], style);
		}

	}

	private void drawThinLine(double x1, double y1, double x2, double y2) throws Exception {
		drawLine(x1, y1, x2, y2, LineStyle.THIN);
	}

	private void drawLine(int x1, int y1, int x2, int y2) throws Exception {
		drawLine(x1, y1, x2, y2, LineStyle.NORMAL);
	}

	private void drawLine(double x1, double y1, double x2, double y2, LineStyle style) throws Exception {
		y1 = INVERT - y1;
		y2 = INVERT - y2;

		Line l = new Line(x1, y1, x2, y2);
		double width = 1.0;
		switch (style) {
		case THIN:
			width = 0.38;
			break;
		case THICK:
			width = 1.45;
			break;
		default:
			width = 1.0;
			break;
		}
		l.setWidth(width);
		l.drawOn(page);
	}

	private void drawText(double x, double y, String string, Font font) throws Exception {
		TextLine text = new TextLine(font, string);
		y = INVERT - y;
		text.setPosition(x, y);
		text.drawOn(page);
	}

	//
	// text.setFont(f2);
	// text.setPosition(70.0, 80.0);
	// text.drawOn(page);
	//
	//
	// Line l2 = new Line(20.0,30.0,30.0,20.0);
	// l2.setWidth(1.45);
	// l2.drawOn(page);
	//
	//

	// public static void main(String[] args) {
	// PdfPrinter.printToStream(null);
	// }
}