ReservationReportPdfStamperView extends AbstractPdfStamperView { @Override protected void mergePdfDocument(Map<String, Object> model, PdfStamper stamper, HttpServletRequest request, HttpServletResponse response) throws Exception { DownloadPDFOutput downloadPDFOutput = (DownloadPDFOutput) model.get( "downloadPDFOutput"); String downloadPDFName = (String) model.get("downloadPDFName"); AcroFields form = stamper.getAcroFields(); String referenceName = downloadPDFOutput.getReferenceName(); if (referenceName.getBytes(StandardCharsets.UTF_8 .name()).length >= REFERENCE_NAME_MAXIMUM_VALUE_WITH_NORMAL_FONTSIZE) { form.setFieldProperty("referenceName", "textsize", REFERENCE_NAME_VARIABLE_FONTSIZE, null); } form.setField("referenceName", referenceName); form.setField("referenceEmail", downloadPDFOutput.getReferenceEmail()); form.setField("referenceTel", downloadPDFOutput.getReferenceTel()); form.setField("paymentMethod", downloadPDFOutput.getPaymentMethod()); form.setField("paymentCompanyName", downloadPDFOutput .getPaymentCompanyName()); form.setField("paymentAccount", downloadPDFOutput.getPaymentAccount()); form.setField("childCount", String.valueOf(downloadPDFOutput .getChildCount())); form.setField("tourName", downloadPDFOutput.getTourName()); form.setField("accomName", downloadPDFOutput.getAccomName()); form.setField("customerKana", downloadPDFOutput.getCustomerKana()); form.setField("customerTel", downloadPDFOutput.getCustomerTel()); form.setField("adultUnitPrice", String.valueOf(downloadPDFOutput .getAdultUnitPrice())); form.setField("reservedDay", sdf.format(downloadPDFOutput .getReservedDay())); form.setField("conductor", downloadPDFOutput.getConductor()); form.setField("tourAbs", downloadPDFOutput.getTourAbs()); form.setField("customerAdd", downloadPDFOutput.getCustomerAdd()); form.setField("customerJob", downloadPDFOutput.getCustomerJob()); form.setField("tourDays", downloadPDFOutput.getTourDays()); form.setField("depDay", sdf.format(downloadPDFOutput.getDepDay())); form.setField("customerName", downloadPDFOutput.getCustomerName()); form.setField("childUnitPrice", String.valueOf(downloadPDFOutput .getChildUnitPrice())); form.setField("depName", downloadPDFOutput.getDepName()); form.setField("customerBirth", sdf.format(downloadPDFOutput .getCustomerBirth())); form.setField("arrName", downloadPDFOutput.getArrName()); form.setField("customerMail", downloadPDFOutput.getCustomerMail()); form.setField("adultCount", String.valueOf(downloadPDFOutput .getAdultCount())); form.setField("customerCode", downloadPDFOutput.getCustomerCode()); form.setField("reserveNo", downloadPDFOutput.getReserveNo()); form.setField("remarks", downloadPDFOutput.getRemarks()); form.setField("accomTel", downloadPDFOutput.getAccomTel()); form.setField("customerPost", downloadPDFOutput.getCustomerPost()); form.setField("printDay", sdf.format(downloadPDFOutput.getPrintDay())); form.setField("adultPrice", String.valueOf(downloadPDFOutput .getAdultPrice())); form.setField("childPrice", String.valueOf(downloadPDFOutput .getChildPrice())); form.setField("sumPrice", String.valueOf(downloadPDFOutput .getSumPrice())); form.setField("paymentTimeLimit", downloadPDFOutput .getPaymentTimeLimit()); stamper.setFormFlattening(true); stamper.setFreeTextFlattening(true); response.setHeader("Content-Disposition", "attachment; filename=" + downloadPDFName + DOWNLOAD_PDF_EXTENSION); }  @Override String getUrl();  }
@Test public void testMergePdfDocument() throws Exception { DownloadPDFOutput downloadPDFOutput = new DownloadPDFOutput(); downloadPDFOutput.setReferenceName("TERASOLUNA TRAVEL CUSTOMER CENTER"); downloadPDFOutput.setReferenceEmail("customer@example.com"); downloadPDFOutput.setReferenceTel("01-2345-6789"); downloadPDFOutput.setPaymentMethod("Bank Transfer"); downloadPDFOutput.setPaymentAccount("Current Account：12345678"); downloadPDFOutput.setChildCount(3); downloadPDFOutput.setTourName("【おすすめ】Terasolunaツアー(6泊7日)"); downloadPDFOutput.setAccomName("TERASOLUNAホテル第一荘"); downloadPDFOutput.setCustomerKana("キムラ タロウ"); downloadPDFOutput.setCustomerTel("111-1111-1111"); downloadPDFOutput.setAdultUnitPrice(75000); downloadPDFOutput.setConductor("Yes"); downloadPDFOutput.setTourAbs( "そこは別天地、静寂と湯けむりの待つ宿へ… 詳しい情報はお取り合わせをお願い致します。"); downloadPDFOutput.setCustomerAdd("千葉県八千代市上高野"); downloadPDFOutput.setCustomerJob("プログラマ"); downloadPDFOutput.setTourDays("7"); downloadPDFOutput.setCustomerName("木村 太郎"); downloadPDFOutput.setChildUnitPrice(37500); downloadPDFOutput.setDepName("北海道"); downloadPDFOutput.setArrName("北海道"); downloadPDFOutput.setCustomerMail("tarou@example.com"); downloadPDFOutput.setAdultCount(5); downloadPDFOutput.setCustomerCode("00000001"); downloadPDFOutput.setReserveNo("00000001"); downloadPDFOutput.setRemarks("特になし"); downloadPDFOutput.setAccomTel("018-123-4567"); downloadPDFOutput.setCustomerPost("276-0022"); downloadPDFOutput.setAdultPrice(375000); downloadPDFOutput.setChildPrice(112500); downloadPDFOutput.setSumPrice(487500); downloadPDFOutput.setPaymentTimeLimit("2019/03/24"); downloadPDFOutput.setReservedDay(SDF.parse("2019/02/21")); downloadPDFOutput.setDepDay(SDF.parse("2019/03/31")); downloadPDFOutput.setCustomerBirth(SDF.parse("1975/01/05")); downloadPDFOutput.setPrintDay(SDF.parse("2019/03/06")); Map<String, Object> model = new HashMap<String, Object>(); model.put("downloadPDFOutput", downloadPDFOutput); model.put("downloadPDFName", "reservationReport"); PdfStamper stamper = new PdfStamper(new PdfReader(RESERVATION_REPORT_PDF_PLACE), new ByteArrayOutputStream(OUTPUT_BYTE_ARRAY_INITIAL_SIZE)); HttpServletRequest request = new MockHttpServletRequest(); HttpServletResponse response = new MockHttpServletResponse(); try { reservationReportPdfStamperView.mergePdfDocument(model, stamper, request, response); } catch (Exception e) { e.printStackTrace(); fail(); } AcroFields form = stamper.getAcroFields(); assertThat(form.getFields().size(), is(35)); assertThat(form.getField("referenceName"), is( "TERASOLUNA TRAVEL CUSTOMER CENTER")); AcroFields.Item referenceNameItem = form.getFieldItem("referenceName"); PdfDictionary referenceNameMerged = referenceNameItem.getMerged(0); TextField referenceNameTextField = new TextField(null, null, null); form.decodeGenericDictionary(referenceNameMerged, referenceNameTextField); assertThat(referenceNameTextField.getFontSize(), is( REFERENCE_NAME_VARIABLE_FONTSIZE)); assertThat(form.getField("referenceEmail"), is("customer@example.com")); assertThat(form.getField("referenceTel"), is("01-2345-6789")); assertThat(form.getField("paymentMethod"), is("Bank Transfer")); assertThat(form.getField("paymentAccount"), is( "Current Account：12345678")); assertThat(form.getField("childCount"), is("3")); assertThat(form.getField("tourName"), is("【おすすめ】Terasolunaツアー(6泊7日)")); assertThat(form.getField("accomName"), is("TERASOLUNAホテル第一荘")); assertThat(form.getField("customerKana"), is("キムラ タロウ")); assertThat(form.getField("customerTel"), is("111-1111-1111")); assertThat(form.getField("adultUnitPrice"), is("75000")); assertThat(form.getField("reservedDay"), is("2019/02/21")); assertThat(form.getField("conductor"), is("Yes")); assertThat(form.getField("tourAbs"), is( "そこは別天地、静寂と湯けむりの待つ宿へ… 詳しい情報はお取り合わせをお願い致します。")); assertThat(form.getField("customerAdd"), is("千葉県八千代市上高野")); assertThat(form.getField("customerJob"), is("プログラマ")); assertThat(form.getField("tourDays"), is("7")); assertThat(form.getField("depDay"), is("2019/03/31")); assertThat(form.getField("customerName"), is("木村 太郎")); assertThat(form.getField("childUnitPrice"), is("37500")); assertThat(form.getField("depName"), is("北海道")); assertThat(form.getField("customerBirth"), is("1975/01/05")); assertThat(form.getField("arrName"), is("北海道")); assertThat(form.getField("customerMail"), is("tarou@example.com")); assertThat(form.getField("adultCount"), is("5")); assertThat(form.getField("customerCode"), is("00000001")); assertThat(form.getField("reserveNo"), is("00000001")); assertThat(form.getField("remarks"), is("特になし")); assertThat(form.getField("accomTel"), is("018-123-4567")); assertThat(form.getField("customerPost"), is("276-0022")); assertThat(form.getField("printDay"), is("2019/03/06")); assertThat(form.getField("adultPrice"), is("375000")); assertThat(form.getField("childPrice"), is("112500")); assertThat(form.getField("sumPrice"), is("487500")); assertThat(form.getField("paymentTimeLimit"), is("2019/03/24")); Object pdfStamperImp = ReflectionTestUtils.getField(stamper, "stamper"); assertThat((boolean) ReflectionTestUtils.getField(pdfStamperImp, "flat"), is(true)); assertThat((boolean) ReflectionTestUtils.getField(pdfStamperImp, "flatFreeText"), is(true)); assertThat(response.getHeader("Content-Disposition"), is( "attachment; filename=reservationReport.pdf")); }
@Test public void testMergePdfDocumentDefaultFontsize() throws Exception { DownloadPDFOutput downloadPDFOutput = new DownloadPDFOutput(); downloadPDFOutput.setReferenceName("TERASOLUNA TRAVEL"); downloadPDFOutput.setReservedDay(SDF.parse("2019/02/21")); downloadPDFOutput.setDepDay(SDF.parse("2019/03/31")); downloadPDFOutput.setCustomerBirth(SDF.parse("1975/01/05")); downloadPDFOutput.setPrintDay(SDF.parse("2019/03/06")); Map<String, Object> model = new HashMap<String, Object>(); model.put("downloadPDFOutput", downloadPDFOutput); model.put("downloadPDFName", "reservationReport"); PdfStamper stamper = new PdfStamper(new PdfReader(RESERVATION_REPORT_PDF_PLACE), new ByteArrayOutputStream(OUTPUT_BYTE_ARRAY_INITIAL_SIZE)); HttpServletRequest request = new MockHttpServletRequest(); HttpServletResponse response = new MockHttpServletResponse(); try { reservationReportPdfStamperView.mergePdfDocument(model, stamper, request, response); } catch (Exception e) { e.printStackTrace(); fail(); } AcroFields form = stamper.getAcroFields(); assertThat(form.getField("referenceName"), is("TERASOLUNA TRAVEL")); AcroFields.Item referenceNameItem = form.getFieldItem("referenceName"); PdfDictionary referenceNameMerged = referenceNameItem.getMerged(0); TextField referenceNameTextField = new TextField(null, null, null); form.decodeGenericDictionary(referenceNameMerged, referenceNameTextField); assertThat(referenceNameTextField.getFontSize(), is( REFERENCE_NAME_DEFAULT_FONTSIZE)); }