Giter Site home page Giter Site logo

flyingsaucer's Introduction

Flying Saucer Download License: LGPL v2.1

OVERVIEW

Flying Saucer is a pure-Java library for rendering arbitrary well-formed XML (or XHTML) using CSS 2.1 for layout and formatting, output to Swing panels, PDF, and images.

Comprehensive documentation available in The Flying Saucer User's Guide.

If you use Flying Saucer in a project, please tell us; it helps suggest directions for the code and may inspire other developers.

LICENSE

Flying Saucer is distributed under the LGPL. Flying Saucer itself is licensed under the GNU Lesser General Public License, version 2.1 or later, available at http://www.gnu.org/copyleft/lesser.html. You can use Flying Saucer in any way and for any purpose you want as long as you respect the terms of the license. A copy of the LGPL license is included as LICENSE-LGPL-2.1.txt or LICENSE-LGPL-3.txt in our distributions and in our source tree.

Flying Saucer uses a couple of FOSS packages to get the job done. A list of these, along with the license they each have, is listed in the LICENSE file in our distribution.

GETTING FLYING SAUCER

New releases of Flying Saucer are distributed through Maven. The available artifacts are:

  • org.xhtmlrenderer:flying-saucer-core - Core library and Java2D rendering
  • org.xhtmlrenderer:flying-saucer-pdf - PDF output using OpenPDF (ex. iText 2.x)
  • org.xhtmlrenderer:flying-saucer-pdf-itext5 - PDF output using iText 5.x (iText 5 is EOL)
  • org.xhtmlrenderer:flying-saucer-pdf-openpdf - not supported anymore (replaced by flying-saucer-pdf)
  • org.xhtmlrenderer:flying-saucer-swt - SWT output
  • org.xhtmlrenderer:flying-saucer-log4j - Logging plugin for log4j

Flying Saucer from version 9.5.0, requires Java 11 or later. Flying Saucer from version 9.6.0, requires Java 17 or later.

GETTING STARTED

See the sample code under the flying-saucer-examples directory.

flying-saucer-core, flying-saucer-pdf, and flying-saucer-swt must be on the build path, as well as the SWT JAR for your OS.

org.xhtmlrenderer.demo.browser.BrowserStartup will start the browser demo.

Other notable entry points include:

  • org.xhtmlrenderer.simple.XHTMLPanel
  • org.xhtmlrenderer.simple.PDFRenderer
  • org.xhtmlrenderer.simple.ImageRenderer

CONTACT

See the discussion group for ongoing discussions.

PROJECT STATUS

Excerpt from the latest merge/release policy:

If you create a PR that can merge without conflicts, I'll merge it. I might ask for additional changes (or feedback from others), but I expect that will be extremely rare.

The basic contract is this: If you're still using Flying Saucer, need something changed, and are willing to do the work yourself, the change will be accepted and released.

Along that line, please also let me know if you need a release done right away. Otherwise, I'll bundle up the changes and do a release every four to six months as long as it's required.

This is also the reason why Issues are currently deactivated on GitHub.

HISTORIC LINKS

flyingsaucer's People

Contributors

aksndr avatar andreasrosdal avatar asolntsev avatar avinash10584 avatar basisbit avatar beckje01 avatar dependabot[bot] avatar dmap avatar ebruchez avatar evermind-micw avatar gaboso avatar gmottram avatar huxi avatar jensrutschmann avatar jklingst avatar liias avatar loesak avatar lzaruba avatar micahhainline avatar nedjs avatar ondrejspanel avatar pbrant avatar pdoubleya avatar rosdal avatar stanio avatar tgeese avatar tobymckoi avatar vianney avatar vmichaud avatar yrodiere avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flyingsaucer's Issues

Support for CSS Level 3 Modules

There are several powerful features in newer CSS modules that would be great to take advantage of in this application, particularly colors with transparency/opacity. Has there been any effort or thoughts on supporting some more recent CSS modules?

Remove various old packages and libraries

I recommend removing these old packages and libraries. By removing flying-saucer-pdf-itext5 and flying-saucer-swt there will be a smaller, more maintainable code-base, which will ease the modernization of this library, eg. support modern HTML 5 etc. Also remove the jar files in lib/dev, these jar files should be included by Maven if they are needed.

https://github.com/flyingsaucerproject/flyingsaucer/tree/main/lib/dev
https://github.com/flyingsaucerproject/flyingsaucer/tree/main/lib

https://github.com/flyingsaucerproject/flyingsaucer/tree/main/flying-saucer-pdf-itext5 (iText 5 is EOL)
https://github.com/flyingsaucerproject/flyingsaucer/tree/main/flying-saucer-swt (SWT is not used much, could be removed or
forked out to a separate project under https://github.com/flyingsaucerproject)
https://github.com/flyingsaucerproject/flyingsaucer/tree/main/flying-saucer-swt-examples

How skip HTML validation while generating PDF?

Reported by (Ezhil](mailto:[email protected])

Team - I am trying to create a PDF using page url. But I am getting an error saying that

Can't load the XML resource (using TrAX transformer). org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 14; Open quote is expected for attribute "name" associated with an element type "meta".

It looks like renderer.setDocument(urlcheck) check whether the URL has proper start and end HTML tag. Is there any we can skip this validation ?

try {
  // Define the URL
  String urlcheck = "https://en.wikipedia.org/wiki/IPhone_15";

  // Establish a URL connection
  HttpURLConnection connection = (HttpURLConnection) new URL(urlcheck).openConnection();
  connection.setRequestMethod("GET");

  // Check the response code (200 indicates success)
  int responseCode = connection.getResponseCode();
  if (responseCode == 200) {
    // Get the input stream from the connection
    InputStream urlInputStream = connection.getInputStream();

    // Create an ITextRenderer instance
    ITextRenderer renderer = new ITextRenderer();

    // Set the HTML content as the document
    renderer.setDocument(urlcheck);

    // Render to PDF
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    renderer.layout();
    renderer.createPDF(outputStream);
    renderer.finishPDF();
  } 

getFormFields for XhtmlForm

Hi. Please add

 public FormField[] getFormFields () {
   Object[] objects = _componentCache.values ().toArray ();
   FormField[] fields = new FormField[objects.length];
   System.arraycopy (objects, 0, fields, 0, objects.length);
   return fields;
   }

to org.xhtmlrenderer.simple.extend.XhtmlForm.

I use this to get the form values without having to submit the entire form.

Cheers,
Jörn

Google fonts not working

I have tried multiple ways to add Google fonts to the pdf but nothing worked for me.

Can you help me how to add the fonts style to pdf?

Application build fails using Flying Saucer Core 9.4.1

Replicate

Install JDK 21 and Gradle 8.5 (add to the PATH), then:

cd /tmp
git clone https://gitlab.com/DaveJarvis/KeenWrite keenwrite
cd keenwrite
gradle clean jar

The build succeeds. Then change 9.3.1 to 9.4.1, such as:

sed -i 's/flying-saucer-core:9.3.1/flying-saucer-core:9.4.1/' build.gradle
gradle clean jar

The build fails with:

> Task :compileJava FAILED
error: cannot access ParametersAreNonnullByDefault
  class file for javax.annotation.ParametersAreNonnullByDefault not found
Note: Some input files use preview features of Java SE 21.
Note: Recompile with -Xlint:preview for details.
1 error

FAILURE: Build failed with an exception.

AccessControlException in Applets

Hi.

Please modifiy

private void updateSystemSelection() {
if (this.dotInfo != this.markInfo && panel != null) {
Clipboard clip = panel.getToolkit().getSystemSelection();
if (clip != null) {
String selectedText = lastHighlightedString;
try {
clip.setContents(new StringSelection(selectedText), null);
} catch (IllegalStateException ise) {
// clipboard was unavailable
// no need to provide error feedback to user since updating
// the system selection is not a user invoked action
}
}
}
}

in org.xhtmlrenderer.swing.SelectionHighlighter to

private void updateSystemSelection() {
if (this.dotInfo != this.markInfo && panel != null)
try {
Clipboard clip = panel.getToolkit().getSystemSelection();
if (clip != null) {
String selectedText = lastHighlightedString;
try {
clip.setContents(new StringSelection(selectedText), null);

            } catch (IllegalStateException ise) {
                // clipboard was unavailable
                // no need to provide error feedback to user since updating
                // the system selection is not a user invoked action
            }
        }
    } catch (AccessControlException t) { }
}

so you won't get a SecurityException when running XHTMLRenderer in an unsigned applet.

Cheers,
Jörn

Adding an image to the full size of a pdf page

I have a problem adding an image to the entire size of a pdf page. I need to make a report in which the first page will be a special image, in a4 format and horizontal orientation (like the entire report).

And no matter what I did, what advice I tried from the Internet (stackoverflow, chatgpt), I was never able to do this simple operation.

I made a project on Github https://github.com/happydroid/pdf_full_page that generates a simplified report consisting of two pages - the first page with an image and the second page with some data. As you can see, trying to render the image in landscape orientation creates three whole pages:
image

As you can see, the page cannot be displayed completely, due to some indentation on the left and possibly on other sides, and I have turned off any indentation(paddings) in the styles for this page.
In the test project, I added the simplest example of rendering the project; I did not add all kinds of non-working methods.

Do you have any ideas on how this can be implemented using your library? Thank you.

Support for HTML 5

FS should support HTML 5.

To update the flyingsaucerproject/flyingsaucer library for essential HTML5 support, focus on key areas that are most impactful for modern web document standards: (chatgpt suggestions:)

  1. HTML5 Parsing: Integrate an HTML5-compliant parser to accurately handle HTML5 documents. This is crucial for recognizing new semantic elements and properly parsing the document structure.

  2. CSS3 Enhancements: Update the CSS rendering engine to support important CSS3 features such as flexbox for layout, media queries for responsive design, and transitions for visual effects. These are foundational for modern web design practices.

  3. Semantic Elements Support: Specifically target support for new semantic elements like <article>, <section>, <nav>, <header>, <footer>, and <figure>. Ensuring these elements are correctly interpreted and rendered is essential for modern web documents.

  4. Form Controls and Input Types: Enhance support for the new form elements and input types introduced in HTML5. This includes types like email, date, range, and color, which are increasingly used in web forms.

  5. JavaScript Interface: Since HTML5 relies on JavaScript for dynamic content, consider how flyingsaucer might either interface with JavaScript or provide hooks for external JavaScript interaction, especially for form validation and handling new input types.

  6. Test Suite for HTML5: Develop a targeted test suite focusing on HTML5 features to ensure compatibility and adherence to standards. Utilize parts of the W3C HTML5 Test Suite for comprehensive coverage.

  7. Documentation and Modular Approach: Update documentation to reflect the support for HTML5 and consider a modular approach for HTML5 features, allowing users to enable specific functionalities as needed. This strategy helps in managing performance implications and maintains backward compatibility.

By concentrating on these aspects, flyingsaucer can significantly improve its HTML5 support, aligning it with current web standards and enhancing its utility for modern web document rendering.

Integrating an HTML5-compliant parser into the flyingsaucerproject/flyingsaucer library involves several detailed steps to ensure accurate handling of HTML5 documents. These steps are crucial for recognizing new semantic elements and properly parsing the document structure:

  1. Evaluate Existing Parser: Assess the capabilities and limitations of the current parsing mechanism in flyingsaucer to understand how it handles HTML and where it falls short with HTML5 content.

  2. Select an HTML5 Parser: Choose an HTML5-compliant parser that can be integrated into flyingsaucer. Popular Java-based parsers like Jsoup or HTMLUnit have strong support for HTML5 and offer a good balance between performance and ease of use.

https://www.w3.org/TR/2011/WD-html5-20110405/
https://html.spec.whatwg.org/

Possibly some implementation details can be copied from:
https://github.com/openhtmltopdf/openhtmltopdf/

Support PDF digital signature

Any chance of including the support for PDF digital signature? If you think this makes sense I could contribute with a pull request

https://helpx.adobe.com/acrobat/using/signing-pdfs.html

I would prepare a pull request for this that would implement a
org.xhtmlrenderer.simple.PDFSigner
similar as
org.xhtmlrenderer.simple.PDFRenderer

That would encapsulate https://api.itextpdf.com/iText5/java/5.5.13.3/com/itextpdf/text/pdf/security/MakeSignature.html

Or would you suggest a better aproach?

FlyingSaucer can cleanup invalid html

FlyingSaucer could be more tolerant to minor errors in html source.
Instead of throwing an exception, say, for non-closed html5 tags, it could close them automatically.

For example, we could use JSoup for job.

Triggered in discussion #277

CJK characters not rendered from Unicode font

Hello,

It seems that I'm not able to generate a PDF where the HTML content contains mixed western/CJK characters. I'm using a Unicode font (Arial Unicode MS), which contains all of the glyphs, but the resulting PDF does not display the CJK characters. I can't tell if it's dropping them or replacing them with something else.

I have a full example project here: https://github.com/emcintyre-hpe/flying-saucer-cjk. HOWEVER, because it uses a proprietary font, the repo is private. Please let me know which contributors here should have access and I will add them.

The example HTML looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta content="text/html"/>
  <style type="text/css">
      body {
          font-family: "ArialUnicodeMS", sans-serif;
      }

      p.cjk {
          font-family: "HeiseiKakuGo-W5-H";
      }
  </style>
  <title>Simple Document</title>
</head>
<body>
<h1>Some Lorem Jeffsum</h1>
<p>Yeah, but John, if The Pirates of the Caribbean breaks down, the pirates don't eat the tourists. God creates
  dinosaurs. God destroys dinosaurs. God creates Man. Man destroys God. Man creates Dinosaurs. I was part of something
  special. Jaguar shark! So tell me - does it really exist?</p>
<p>Checkmate... This thing comes fully loaded. AM/FM radio, reclining bucket seats, and... power windows. Do you have
  any idea how long it takes those cups to decompose. I was part of something special. Eventually, you do plan to have
  dinosaurs on your dinosaur tour, right?</p>
<h2>Some Chinese text from Google Translate</h2>
<p>这东西满载而归。 AM/FM 收音机、斜躺桶形座椅和……电动车窗。 我们必须焚烧雨林、倾倒有毒废物、污染空气、破坏臭氧层!
  因为也许如果我们把这个星球搞得够糟,他们就不再想要它了!</p>
<h2>Some Japanese text from Google Translate</h2>
<p>太った女性のことは忘れてください! あなたは太った女性に夢中です!English text interspersed with Japanese. 私たちをここから追い出してください!
  もっと早く行かなければ...行け、行け、行け、行け、行け! 父がかつて私にこう言いました、笑えば世界もあなたと一緒に笑います、泣きなさい、そうすればこの野郎のことで泣けるようにしてあげますよ!
  最終的には、恐竜ツアーに恐竜を参加させる予定ですよね?</p>
<h2>Some Korean text from Google Translate</h2>
<p>결국에는 공룡 투어에 공룡도 함께 할 계획이시죠? 운이 좋아서 얼음이 없어요. 이게 내 에스프레소 머신인가요? 뭐, 뭐야, 뭐야, 내 에스프레소 머신은 어떻게 샀어? 응, 하지만 존, 캐리비안의 해적이 망하면
  해적들은 관광객을 잡아먹지 않아.</p>
<h2>The Japanese text below uses a dedicated Japanese font</h2>
<p class="cjk">太った女性のことは忘れてください! あなたは太った女性に夢中です! 私たちをここから追い出してください!
  もっと早く行かなければ...行け、行け、行け、行け、行け! 父がかつて私にこう言いました、笑えば世界もあなたと一緒に笑います、泣きなさい、そうすればこの野郎のことで泣けるようにしてあげますよ!
  最終的には、恐竜ツアーに恐竜を参加させる予定ですよね?</p>
</body>
</html>

The transformation code:

import static org.apache.commons.io.IOUtils.resourceToString;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.xhtmlrenderer.pdf.CJKFontResolver;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xhtmlrenderer.simple.xhtml.XhtmlNamespaceHandler;

import com.lowagie.text.pdf.BaseFont;

/**
 * Run with {@code mvn compile exec:java -Dexec.mainClass="CjkExample"}
 */
public class CjkExample {
  public static void main(String[] args) throws IOException {
    // Create renderer
    ITextRenderer pdfRenderer = initRenderer();
    // Create output stream
    try (OutputStream pdf = Files.newOutputStream(Paths.get("target/example.pdf"))) {
      // Read HTML file
      String html = resourceToString("/example.html", StandardCharsets.UTF_8);
      pdfRenderer.setDocumentFromString(html);
      pdfRenderer.layout();
      pdfRenderer.createPDF(pdf, true);
    }
  }

  private static ITextRenderer initRenderer() throws IOException {
    ITextFontResolver fonts = new CJKFontResolver();
    ITextRenderer renderer = new ITextRenderer(fonts);
    fonts.addFont("/fonts/ArialUnicodeMS.ttf", "ArialUnicodeMS", BaseFont.CP1252, true, null);
    renderer.getSharedContext().setNamespaceHandler(new XhtmlNamespaceHandler());
    return renderer;
  }
}

And here is the resulting PDF

Running elements only rendered on last page

Hello,

I want to use flying saucer to get rid of some other PDF generating library and I am really happy to see that it supports margin boxes and running elements! The margin boxes seem to be working fine as long as I stick to content: "..." but if I switch to running elements, like a div somewhere in the page with a class, which I give position: running(NAME) and then apply to a margin box using content: element(NAME), it appears that the div is only rendered on the final page instead of on every page.

Is this a possible bug or does it sound like I am doing something wrong?

Font ascent and descent in ITextTextRenderer

In the method getFSFontMetrics of ITextTextRenderer (line 55), BaseFont.BBOXURY and BaseFont.BBOXLLY are used to calculate the ascent and descent of the font while this values are provided by iText.
I propose the following change :
result.setAscent(bf.getFontDescriptor(BaseFont.ASCENT, size));
result.setDescent(-bf.getFontDescriptor(BaseFont.DESCENT, size));
With this, the markers of list items are better vertically centered.

CSS: page-break-before "always" -- slightly wrong implementation (in PDF)

If you create an xhtml file that has a content something like this:

<style> div.chapter { page-break-before: always; }
Test
--- The generated PDF creates an empty page. The div starts at page 2. However, according to the CSS3 paged-media specs, a page-break-before: always should not create empty pages. Neither should page-break-after: always! It is correct that you always break to a new page. But creating "blank" pages is an exception to this rule. This causes some annoyances and it would be great if this gets fixed!!

org.xhtmlrenderer.pdf.ITextRenderer.setDocumentFromString - missing 'baseUrl' parameter

to check #276 I tried to upgrade to 9.6.1 (from 9.1.22) causing my code to fail.
we are trying to generate pdf from a html that's generated in memory.

we then used to call renderer.setDocument(Document doc, String url)

that method seems to be private now.
all alternatives don't allow to pass a baseUrl parameter, causing relative URLs (CSS) to fail.

is there any other alternative?

No way to set Document directly in renderer

9.6.0 made most of setDocument methods private. I still need to use an option to provide Document object instead of String. This happened here (if I'm to believe release changelog): #281

In my case parsing html inside renderer causes an exception to be thrown. I use a more forgiving parser:

            Document document = Jsoup.parse(htmlData);
            document.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
            document.outputSettings().charset(StandardCharsets.UTF_8);
            ...
            renderer.setDocument(new W3CDom().fromJsoup(document), "/");

CSS and Image is not rendering in generated PDF

(reported by Ezhil [email protected])

Team - I want to make a PDF for my webpage using Flying Saucer to generate a PDF. With this below code I am able to generate a PDF but without any style and css.

I am trying to set my base URL using renderer.getSharedContext().setUserAgentCallback(new NaiveUserAgent(baseUrl)) but it is not working. it expects int type. Can anyone tell how to solve CSS and Images on the PDF ?

try {
        // Define the URL
        String urlcheck = "http://localhost:8888/index.html";
       
        // Establish a URL connection
        HttpURLConnection connection = (HttpURLConnection) new URL(urlcheck).openConnection();
        connection.setRequestMethod("GET");

        // Set up Basic Authentication with username "admin" and password "admin"
        String username = "admin";
        String password = "admin";
        String authString = username + ":" + password;
        String encodedAuthString = Base64.getEncoder().encodeToString(authString.getBytes());

        connection.setRequestProperty("Authorization", "Basic " + encodedAuthString);

        // Check the response code (200 indicates success)
        int responseCode = connection.getResponseCode();
        if (responseCode == 200) {

          // Get the input stream from the connection
          InputStream urlInputStream = connection.getInputStream();

          LOG.info("PDF START ");
          // Create an ITextRenderer instance
          ITextRenderer renderer = new ITextRenderer();

          // Convert the InputStream to a String
          String htmlContent = convertInputStreamToString(urlInputStream);
          HtmlCleaner cleaner = new HtmlCleaner();
          TagNode rootTagNode = cleaner.clean(htmlContent);

          // set up properties for the serializer (optional, see online docs)
          CleanerProperties cleanerProperties = cleaner.getProperties();

          // use the getAsString method on an XmlSerializer class
          XmlSerializer xmlSerializer = new PrettyXmlSerializer(cleanerProperties);
          String cleanedHtml = xmlSerializer.getAsString(rootTagNode);
          LOG.info("cleanedHtml::"+cleanedHtml);

          // Set the HTML content as the document
          renderer.setDocumentFromString(cleanedHtml);

          // Render to PDF
          String baseUrl = "http://localhost:8888/";
          renderer.getSharedContext().setUserAgentCallback(new NaiveUserAgent(baseUrl)); // Error

          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
          renderer.layout();
          renderer.createPDF(outputStream);
          renderer.finishPDF();

        } else {
          LOG.error("Failed to retrieve URL content. Response code: " + responseCode);
        }
      }

table-cell float:left fails PDF generation

for whatever-reason, a HTML/CSS I cannot fully control, applies float:left to a table-cell.
this results in the cell being rendered not as table-cell, but block.

this fails PDF generation with following error:

java.lang.ClassCastException: class org.xhtmlrenderer.render.AnonymousBlockBox cannot be cast to class org.xhtmlrenderer.newtable.TableCellBox (org.xhtmlrenderer.render.AnonymousBlockBox and org.xhtmlrenderer.newtable.TableCellBox are in unnamed module of loader 'app')
	at org.xhtmlrenderer.newtable.TableSectionBox.recalcCells(TableSectionBox.java:83)
	at org.xhtmlrenderer.newtable.TableBox.recalcSections(TableBox.java:204)
	at org.xhtmlrenderer.newtable.TableBox.calcMinMaxWidth(TableBox.java:154)
	at org.xhtmlrenderer.newtable.TableBox.layout(TableBox.java:221)
	at org.xhtmlrenderer.layout.BlockBoxing.layoutBlockChild0(BlockBoxing.java:321)
	at org.xhtmlrenderer.layout.BlockBoxing.layoutBlockChild(BlockBoxing.java:299)
	at org.xhtmlrenderer.layout.BlockBoxing.layoutContent(BlockBoxing.java:90)
	at org.xhtmlrenderer.render.BlockBox.layoutChildren(BlockBox.java:978)
	at org.xhtmlrenderer.render.BlockBox.layout(BlockBox.java:858)
	at org.xhtmlrenderer.render.BlockBox.layout(BlockBox.java:787)
	at org.xhtmlrenderer.layout.BlockBoxing.layoutBlockChild0(BlockBoxing.java:321)
	at org.xhtmlrenderer.layout.BlockBoxing.layoutBlockChild(BlockBoxing.java:299)
	at org.xhtmlrenderer.layout.BlockBoxing.layoutContent(BlockBoxing.java:90)
	at org.xhtmlrenderer.render.BlockBox.layoutChildren(BlockBox.java:978)
	at org.xhtmlrenderer.render.BlockBox.layout(BlockBox.java:858)
	at org.xhtmlrenderer.render.BlockBox.layout(BlockBox.java:787)
	at org.xhtmlrenderer.pdf.ITextRenderer.layout(ITextRenderer.java:229)

see following test:
PdfTest.txt

Center image

Is it possible to center image ?

I tried various way to center image and it always printed on the left.

e.g.:

<div style="text-align: center;">
        <img th:src="my-image-file.png" alt="Project logo" src=""/>
        <br/>
        VOYONS CE QUE VOUS AVEZ SÉLECTIONNÉ !
</div>

Doing the same with text and image:
image

ITextRenderer class, but no dependency on IText

In some places, class names contain "iText" (e.g. ITextRenderer), although there seem to be no dependencies on iText. Is this a purely historical origin? Regarding licenses, this is a bit confusing.

Make it possible to add resources with `classpath:` prefix

I can't generate pdf using externally loaded css stylesheet. It is generate without any styles.
Images with classpath prefix are not loaded too.
Everything works when I specify absolute path to file (with prefix file://), but this is not acceptable in my app.

My code:

final ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.getFontResolver().addFontDirectory(properties.fonts(), EMBEDDED);
renderer.layout();
final ByteArrayOutputStream pdf = new ByteArrayOutputStream();
renderer.createPDF(pdf);
write(properties.output(), pdf.toByteArray());

My HTML:

<html lang="pl-PL">
<head>
    <title>Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="Content-Language" content="pl-PL"/>
    <link href="classpath:templates/css/stylesheet.css" rel="stylesheet" type="text/css" media="print"/>
</head>
<body>...</body>
</html>

My maven dependecies:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>pdf-generator</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>pdf-generator</name>
    <description>pdf-generator</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.xhtmlrenderer</groupId>
            <artifactId>flying-saucer-pdf-openpdf</artifactId>
            <version>9.3.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

NPE in ScalableXHTMLPanel

I had posted this issue initially on the code.google.com site, but it seems this one is not used anymore, so I'm trying it here...

Right now the latest code although having some great fixes compared to the code from dev.java.net, is unusable because of the following.

Could you have a look at it, please?

What steps will reproduce the problem?

  1. Using ScalableXHTMLPanel
  2. call getSharedContext().setPrint(true); on instance
  3. resize the panel

What is the expected output?
Should resize normaly.

What do you see instead?
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at org.xhtmlrenderer.swing.ScalableXHTMLPanel.doRender(ScalableXHTMLPanel.java:173)
at org.xhtmlrenderer.swing.BasicPanel.paintComponent(BasicPanel.java:149)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

What version of the product are you using? On what operating system?
Latest master from GitHub. Windows XP.

Please provide any additional information below.
It seems that when using setPrint(true) root.getMaster().getPaintingInfo()
always returns null.

After this exception, the application cannot recover, any subsequent call to repaint() triggers the Exception.

Left and right margin after :first page bug

Hello, good day.
As mentioned in this conversation:
link
There is an error occurring when defining margins on the first page (@page:first) and the default page (@page). The issue is that some blank margins are being generated, as shown in the attached photos. In my case, the same problem occurs. I have the following configuration, and after the first page, these blank "margins" start to appear, breaking the visibility.

I have this configuration, maybe there is something wrong with my code?

        @page :first{
            margin-top: 5mm;
            margin-left: 5mm;
            margin-bottom: 5mm;

            @bottom-right{
                content: none;
            }

            @bottom-left{
                content: none;
            }
        }

        @page {
            margin-top: 3.3cm;
            margin-left: 1.5cm;
            margin-right: 1.5cm;
            margin-bottom: 1.7cm;
           }

image

first page
Screenshot 2023-11-15 at 9 47 21 AM

second page
Screenshot 2023-11-15 at 9 47 31 AM

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.