CSS and Image is not rendering in generated PDF

453 views
Skip to first unread message

Ezhil

unread,
Oct 11, 2023, 12:58:32 PM10/11/23
to Flying Saucer Users
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);
        }
      } 

Andrei Solntsev

unread,
Dec 29, 2023, 5:59:18 AM12/29/23
to flying-sa...@googlegroups.com
Hi Ezhil!
You can use method "setBaseURL":

NaiveUserAgent userAgent = new NaiveUserAgent();
userAgent.setBaseURL(baseUrl);
renderer.getSharedContext().setUserAgentCallback(userAgent); // No error anymore

Andrei Solntsev


ср, 11 окт. 2023 г. в 19:58, Ezhil <ezh...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Flying Saucer Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flying-saucer-u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flying-saucer-users/1b312d55-0461-47c2-b39f-4a4c03b1bf67n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages