Search code examples
javahtmlpdfjakarta-eejodconverter

How prevent editing my pdf using jodconverter?


I am using the jodconverter 3.0-beta4 and I am converting from HTML to PDF. My HTML contains a form, but I don't want to have editable fields in my PDF. I thought that if I could make my pdf be read only then the fields are gone, but my code isnt working.

I already tried with different versions of PDF, but a version below 2 does not reproduce correctly my HTML. Its converts the HTML with some inputs unmarked, but my radio inputs are all marked.

Here is my code

DocumentFormat format = new DocumentFormat("PDF/A", "pdf", "application/pdf");

Map<String, Object> filterData = new HashMap<String, Object>();
filterData.put("SelectPdfVersion", 2);

Map<String, Object> properties = new HashMap<String, Object>();
properties.put("FilterData", filterData);
properties.put("FilterName", "writer_pdf_Export");
properties.put("Changes", 0);
properties.put("RestrictPermissions", true);

format.setStoreProperties(DocumentFamily.TEXT, properties);

OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);

File htmlFile = File.createTempFile("tempHTML" , ".html");
org.apache.commons.io.FileUtils.writeStringToFile(htmlFile, html);
File pdfFile = File.createTempFile("tempPDF", ".pdf");
converter.convert(htmlFile, pdfFile, format);

Solution

  • One might convert the HTML inputs to static text prior to converting it to a PDF.

    As you are already using a String with the HTML content, html:

    Something like:

    // ☐ ☑ ☒ ☉ ◉ ○
    html = html.replaceAll("(?si)<input type=\"radio\"[^>]*value=\"("
            + checkedValue + ")\"[^>]*>(.*?)</input>",
            "◉ $2");
    html = html.replaceAll("(?si)<input type=\"radio\"[^>]*value=\"("
            + "[^\"]+" + ")\"[^>]*>(.*?)</input>",
            "○ $2");
    

    Probably you need to do more, and the above code relies on the type attribute preceding the value attribute etcetera.