|
| 1 | +/* |
| 2 | + This file is part of the iText (R) project. |
| 3 | + Copyright (c) 1998-2024 Apryse Group NV |
| 4 | + Authors: Apryse Software. |
| 5 | +
|
| 6 | + This program is offered under a commercial and under the AGPL license. |
| 7 | + For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below. |
| 8 | +
|
| 9 | + AGPL licensing: |
| 10 | + This program is free software: you can redistribute it and/or modify |
| 11 | + it under the terms of the GNU Affero General Public License as published by |
| 12 | + the Free Software Foundation, either version 3 of the License, or |
| 13 | + (at your option) any later version. |
| 14 | +
|
| 15 | + This program is distributed in the hope that it will be useful, |
| 16 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + GNU Affero General Public License for more details. |
| 19 | +
|
| 20 | + You should have received a copy of the GNU Affero General Public License |
| 21 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | +package com.itextpdf.html2pdf.css; |
| 24 | + |
| 25 | +import com.itextpdf.html2pdf.ExtendedHtmlConversionITextTest; |
| 26 | +import com.itextpdf.html2pdf.HtmlConverter; |
| 27 | +import com.itextpdf.io.logs.IoLogMessageConstant; |
| 28 | +import com.itextpdf.io.util.UrlUtil; |
| 29 | +import com.itextpdf.kernel.pdf.PdfDocument; |
| 30 | +import com.itextpdf.kernel.pdf.PdfReader; |
| 31 | +import com.itextpdf.kernel.pdf.canvas.parser.PdfTextExtractor; |
| 32 | +import com.itextpdf.test.LogLevelConstants; |
| 33 | +import com.itextpdf.test.annotations.LogMessage; |
| 34 | +import com.itextpdf.test.annotations.LogMessages; |
| 35 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 36 | + |
| 37 | +import java.io.File; |
| 38 | +import java.io.IOException; |
| 39 | +import org.junit.Assert; |
| 40 | +import org.junit.BeforeClass; |
| 41 | +import org.junit.Test; |
| 42 | +import org.junit.experimental.categories.Category; |
| 43 | + |
| 44 | +@Category(IntegrationTest.class) |
| 45 | +public class DirectionTest extends ExtendedHtmlConversionITextTest { |
| 46 | + private static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/html2pdf/css/DirectionTest/"; |
| 47 | + private static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/html2pdf/css/DirectionTest/"; |
| 48 | + |
| 49 | + @BeforeClass |
| 50 | + public static void beforeClass() { |
| 51 | + createDestinationFolder(DESTINATION_FOLDER); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + @LogMessages(messages = { |
| 56 | + @LogMessage(messageTemplate = IoLogMessageConstant.TYPOGRAPHY_NOT_FOUND, count = 2, logLevel = |
| 57 | + LogLevelConstants.WARN) |
| 58 | + }) |
| 59 | + public void simpleLtrDocTest() throws IOException { |
| 60 | + Assert.assertTrue(getTextFromDocument(convertToHtmlDocument("SimpleLtrDoc"), |
| 61 | + 1).contains("123456789.")); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void simpleLtrElementDocTest() throws IOException { |
| 66 | + Assert.assertTrue(getTextFromDocument(convertToHtmlDocument("SimpleLtrElementDoc"), |
| 67 | + 1).contains("123456789.")); |
| 68 | + } |
| 69 | + |
| 70 | + //TODO DEVSIX-1920: RTL ignored. Change test after fix |
| 71 | + @Test |
| 72 | + public void simpleRtlElementDocTest() throws IOException { |
| 73 | + Assert.assertFalse(getTextFromDocument(convertToHtmlDocument("SimpleRtlElementDoc"), |
| 74 | + 1).contains(".Right to left text")); |
| 75 | + } |
| 76 | + |
| 77 | + //TODO DEVSIX-2437 : Change test after fix |
| 78 | + @Test |
| 79 | + @LogMessages(messages = { |
| 80 | + @LogMessage(messageTemplate = IoLogMessageConstant.TYPOGRAPHY_NOT_FOUND, count = 4, logLevel = |
| 81 | + LogLevelConstants.WARN) |
| 82 | + }) |
| 83 | + public void ltrInRtlDocTest() throws IOException { |
| 84 | + Assert.assertFalse(getTextFromDocument(convertToHtmlDocument("LtrInRtlDoc"), |
| 85 | + 1).contains("!Right to left text")); |
| 86 | + } |
| 87 | + |
| 88 | + //TODO DEVSIX-2437 : Change test after fix |
| 89 | + @Test |
| 90 | + @LogMessages(messages = { |
| 91 | + @LogMessage(messageTemplate = IoLogMessageConstant.TYPOGRAPHY_NOT_FOUND, count = 4, logLevel = |
| 92 | + LogLevelConstants.WARN) |
| 93 | + }) |
| 94 | + public void rtlInLtrDocTest() throws IOException { |
| 95 | + Assert.assertFalse(getTextFromDocument(convertToHtmlDocument("RtlInLtrDoc"), |
| 96 | + 1).contains("!Right to left text")); |
| 97 | + } |
| 98 | + |
| 99 | + //TODO DEVSIX-3069: Change test after fix |
| 100 | + @Test |
| 101 | + @LogMessages(messages = { |
| 102 | + @LogMessage(messageTemplate = IoLogMessageConstant.TYPOGRAPHY_NOT_FOUND, count = 34, logLevel = |
| 103 | + LogLevelConstants.WARN), |
| 104 | + @LogMessage(messageTemplate = IoLogMessageConstant.TABLE_WIDTH_IS_MORE_THAN_EXPECTED_DUE_TO_MIN_WIDTH, |
| 105 | + count = 1, logLevel = |
| 106 | + LogLevelConstants.WARN) |
| 107 | + }) |
| 108 | + public void bigTableTest() throws IOException, InterruptedException { |
| 109 | + convertToPdfAndCompare("TooLargeTable", SOURCE_FOLDER, DESTINATION_FOLDER); |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + private PdfDocument convertToHtmlDocument(String fileName) throws IOException { |
| 114 | + String sourceHtml = SOURCE_FOLDER + fileName + ".html"; |
| 115 | + String destPdf = DESTINATION_FOLDER + fileName + ".pdf"; |
| 116 | + HtmlConverter.convertToPdf(new File(sourceHtml), new File(destPdf)); |
| 117 | + System.out.println("html: " + UrlUtil.getNormalizedFileUriString(sourceHtml) + "\n" |
| 118 | + + "Out pdf: "+ UrlUtil.getNormalizedFileUriString(destPdf)); |
| 119 | + return new PdfDocument(new PdfReader(destPdf)); |
| 120 | + } |
| 121 | + |
| 122 | + private String getTextFromDocument(PdfDocument document, int pageNum) { |
| 123 | + return PdfTextExtractor.getTextFromPage(document.getPage(pageNum)); |
| 124 | + } |
| 125 | +} |
0 commit comments