-
Notifications
You must be signed in to change notification settings - Fork 613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parse HTML List #290
Merged
Merged
Parse HTML List #290
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
pdf-toolbox/src/test/java/com/lowagie/examples/html/ParseHelloHtml.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* $Id: HelloHtml.java 3373 2008-05-12 16:21:24Z xlv $ | ||
* | ||
* This code is part of the 'OpenPDF Tutorial'. | ||
* You can find the complete tutorial at the following address: | ||
* https://github.com/LibrePDF/OpenPDF/wiki/Tutorial | ||
* | ||
* This code is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* | ||
* | ||
*/ | ||
|
||
package com.lowagie.examples.html; | ||
|
||
import com.lowagie.text.Document; | ||
import com.lowagie.text.DocumentException; | ||
import com.lowagie.text.html.HtmlParser; | ||
import com.lowagie.text.pdf.PdfWriter; | ||
|
||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Generates a simple 'Hello World' HTML page. | ||
* | ||
* @author blowagie | ||
*/ | ||
|
||
public class ParseHelloHtml { | ||
|
||
/** | ||
* Generates an HTML page with the text 'Hello World' | ||
* | ||
* @param args no arguments needed here | ||
*/ | ||
public static void main(String[] args) { | ||
System.out.println("Parse Hello World"); | ||
|
||
// step 1: creation of a document-object | ||
try (Document document = new Document()) { | ||
PdfWriter.getInstance(document, new FileOutputStream("parseHelloWorld.pdf")); | ||
// step 2: we open the document | ||
document.open(); | ||
// step 3: parsing the HTML document to convert it in PDF | ||
HtmlParser.parse(document, ParseHelloHtml.class.getClassLoader().getResourceAsStream("com/lowagie/examples/html/parseHelloWorld.html")); | ||
} catch (DocumentException | IOException de) { | ||
System.err.println(de.getMessage()); | ||
} | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
pdf-toolbox/src/test/java/com/lowagie/examples/html/ParseNestedHtmlList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* $Id: HelloHtml.java 3373 2008-05-12 16:21:24Z xlv $ | ||
* | ||
* This code is part of the 'OpenPDF Tutorial'. | ||
* You can find the complete tutorial at the following address: | ||
* https://github.com/LibrePDF/OpenPDF/wiki/Tutorial | ||
* | ||
* This code is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* | ||
* | ||
*/ | ||
|
||
package com.lowagie.examples.html; | ||
|
||
import com.lowagie.text.DocumentException; | ||
import com.lowagie.text.Element; | ||
import com.lowagie.text.html.simpleparser.HTMLWorker; | ||
import com.lowagie.text.html.simpleparser.StyleSheet; | ||
|
||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Generates a simple 'Hello World' HTML page. | ||
* | ||
* @author blowagie | ||
*/ | ||
|
||
public class ParseNestedHtmlList { | ||
|
||
/** | ||
* Generates an HTML page with the text 'Hello World' | ||
* | ||
* @param args no arguments needed here | ||
*/ | ||
public static void main(String[] args) { | ||
System.out.println("Parse Nested HTML List"); | ||
try { | ||
final String htmlText = | ||
"<html>" | ||
+ "<body>" | ||
+ "<p>What should you say?</p>" | ||
+ "<ul>" | ||
+ " <li>Hello</li>" | ||
+ " <li>World</li>" | ||
+ "</ul>" | ||
+ "<ol>" | ||
+ " <li>Element-1" | ||
+ " <ol>" | ||
+ " <li>Element-1-1</li>" | ||
+ " <li>Element-1-2</li>" | ||
+ " </ol>" | ||
+ " </li>" | ||
+ " <li>Element-2" | ||
+ " <ol>" | ||
+ " <li>Element-2-1" | ||
+ " <ol>" | ||
+ " <li>Element-2-1-1" | ||
+ " <ol>" | ||
+ " <li>Element-2-1-1-1</li>" | ||
+ " <li>Element-2-1-1-2</li>" | ||
+ " </ol>" | ||
+ " </li>" | ||
+ " <li>Element-2-1-2" | ||
+ " <ol>" | ||
+ " <li>Element-2-1-2-1</li>" | ||
+ " <li>Element-2-1-2-2</li>" | ||
+ " </ol>" | ||
+ " </li>" | ||
+ " </ol>" | ||
+ " </li>" | ||
+ " <li>Element-2-2</li>" | ||
+ " </ol>" | ||
+ " </li>" | ||
+ "</ol>" | ||
+ "</body>" | ||
+ "</html>"; | ||
|
||
final StringReader reader = new StringReader(htmlText); | ||
final StyleSheet styleSheet = new StyleSheet(); | ||
final Map<String, Object> interfaceProps = new HashMap<>(); | ||
|
||
final List<Element> elements = HTMLWorker.parseToList(reader, styleSheet, interfaceProps); | ||
printElement("", elements); | ||
|
||
} catch (DocumentException | IOException de) { | ||
System.err.println(de.getMessage()); | ||
} | ||
} | ||
|
||
private static void printElement(String depth, List<Element> elements) { | ||
for (Element element : elements) { | ||
System.out.println(depth + "- element.getClass() = " + element.getClass()); | ||
if (element instanceof com.lowagie.text.List) { | ||
com.lowagie.text.List elementList = (com.lowagie.text.List) element; | ||
printElement(depth + " ", elementList.getItems()); | ||
} else { | ||
System.out.println(depth + " element = " + element.getChunks().get(0).toString()); | ||
} | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
pdf-toolbox/src/test/resources/com/lowagie/examples/html/parseHelloWorld.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<html> | ||
<body> | ||
<p>What should you say?</p> | ||
<ul> | ||
<li>Hello</li> | ||
<li>World</li> | ||
</ul> | ||
<ol> | ||
<li>Element-1 | ||
<ol> | ||
<li>Element-1-1</li> | ||
<li>Element-1-2</li> | ||
</ol> | ||
</li> | ||
<li>Element-2 | ||
<ol> | ||
<li>Element-2-1</li> | ||
<li>Element-2-2</li> | ||
</ol> | ||
</li> | ||
</ol> | ||
</body> | ||
</html> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the difference between test above and this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one uses
HTMLWorker#parseToList()
with a StringReader and produce a list on items.The previous one uses
HtmlParser#parse()
with a Document to produce a PDF.The first one produced a good PDF document with the nested HTML List. But the problem of this issue is really about the
HTMLWorker#parseToList()
not producing the nested List as expected.Event if the first test, producing the PDF, is not showing the bug, I choose to keep it as a Non-Regression Test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But why second example contains HTML markup as a String and not as a file?