MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    HTML with external CSS using iText

    Scheduled Pinned Locked Moved Development
    2 Posts 2 Posters 950 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • F Offline
      foxie
      last edited by

      So Flying Saucer is being used to convert some PDF documents from strings to HTML.

      DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      InputStream is = new ByteArrayInputStream(html.getBytes("UTF-9"));
      Document doc = builder.parse(is);
      
      response.setContentType("application/pdf; charset=UTF-9");
      response.setHeader("Content-disposition", "inline; filename=\"" + outFileName + "\"");
      
      OutputStream os = response.getOutputStream();
      
      ITextRenderer iTextRenderer = new ITextRenderer();
      iTextRenderer.setDocument(doc,null);
      iTextRenderer.layout();
      iTextRenderer.createPDF(os);
      os.flush();
      os.close()
      

      This works perfectly. When I’m working with plain text. In my HTML content, I referenced an external CSS. However, CSS is not applied when the PDF is generated.

      According to what I’ve read, the setDocument() method takes two parameters: document and url. The url parameter specifies the base url that will be prepended to relative paths in the xhtml, such as an external CSS file.

      As a result, I attempted to supply

      context path/css
      

      I included the directory in the baseURL and used it in the setDocument (). There is still no outcome.

      So, here’s my question: What is the proper URL to use as the baseURL?

      String baseURL = ""; // What goes here as root URL for resources
      iTextRenderer.setDocument(doc,baseURL);
      
      S 1 Reply Last reply Reply Quote 0
      • S Offline
        sdetweil @foxie
        last edited by sdetweil

        @foxie the mm base web server is at

        http://machine-address:port_from_config.js
        

        module path adds

        modules/module_name
        

        and any folder inside module folder
        sometimes

        node_modules
        

        and library
        and it’s subfolder

        the

         path 
        

        library

        resolve
        

        method
        can help build that path

        be careful using localhost for the machine address, as this means ONLY. apps INSIDE the same machine can access

        if you ran a remote browser to access your mm with your module, localhost would fail

        I will use localhost for this example , using module name fribble

        let host_address = config.address==“0.0.0.0”?“localhost”:config.address

        “http://”+host_address+“:”+config.port+“/modules/”+this.name+“/node_modules/library_name/folder”

        if all u need is the path to the folder

        I will leave as an exercise how to get the magicmirror machine ip address if the config says ‘any adapter’ (address:“0.0.0.0”)

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Enjoying MagicMirror? Please consider a donation!
        MagicMirror created by Michael Teeuw.
        Forum managed by Sam, technical setup by Karsten.
        This forum is using NodeBB as its core | Contributors
        Contact | Privacy Policy