Creating xml through jsp - html

I want to create an xml file using jsp. The xml file should contain multiple details as in employee details. The value for the xml tag is from html page. Every time the button is clicked, a new set of tags should be created under the employee tag, for each employee.

create a javascript function for the button in your jsp page. For creating the dynamic xml document, in the javascript function include the jsp tag for creating the file with the extension of xml, like this,
PrintWriter writer = new PrintWriter("the-file-name.xml", "UTF-8");
writer.println("<tag1>"+value1+"</tag1>");
writer.println("<tag2>"+value2+"</tag2>");
writer.close();
where values are obtained from the html pagge.

Related

Nested Bookmarks in PDF

I use the HTML to PDF converter from EVO to create a PDF document and I use the following code to generate bookmarks for H tags:
// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
// Select the HTML elements to bookmark by setting a list of CSS selectors
htmlToPdfConverter.PdfBookmarkOptions.HtmlElementSelectors = new string[] { "H1", "H2", "H3" };
// Display the bookmarks panel in PDF viewer when the generated PDF is opened
htmlToPdfConverter.PdfViewerPreferences.PageMode = ViewerPageMode.UseOutlines;
// Convert the HTML page to a PDF document in a memory buffer
byte[] outPdfBuffer = htmlToPdfConverter.ConvertUrl(urlTextBox.Text);
The bookmarks appear in viewer but they are all at the same level. I would like to have them organized in a tree with H1 tags at first level, H2 tags at the second level and so on. I found another software which can organize the bookmarks the way I want, but I would like to avoid using another tool just for this. I want to generate the bookmarks directly in a tree.
There are 2 methods to automatically create bookmarks in generated PDF document. One is through API and is the method you already used but that does no produce a hierarchy of bookmarks as you would like.
The other method is to set the following line in your code:
// Enable the creation of a hierarchy of bookmarks from H1 to H6 tags
htmlToPdfConverter.PdfBookmarkOptions.AutoBookmarksEnabled = true;
This will enable the creation of a tree of bookmarks based on the heading tags in your HTML.

Is it possible to embed a HTML into a PDF?

I like to embed a HTML site into a PDF document. Are there any libraries or PDF creator that make that possible?
Update:
I am not looking for ways to convert a HTML to PDF. I actually want to use the HMTL as it is inside the PDF. So I am looking for something like iframe for PDF.
There are a few out there, depends if you need to build using PHP or another language. I have used MPDF before: http://www.mpdf1.com/mpdf/
Yes, its possible using xmlworker5.4.1.jar. The XML worker object allows you to embed html in your document. xmlString object below is your HTML content as HTMLWorker is deprecated so use XMLWorker only.
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
String currentLine = "";
StringBuffer xmlString = new StringBuffer();
xmlString.append("<html><body>");
String str = htmlPages[i];
xmlString.append(htmlPages[i]);
xmlString.append("</body></html>");
worker.parseXHtml(pdfWriter, pdfDocument, new StringReader(xmlString.toString()));
Inorder to incorporate fonts mentioned in font face tag u need to register fonts using
FontFanctory.redisterDirectory("path of font files");
because itext doesnt scan system for fonts. u need to register it yourself this way

Turn CKEDITOR into a pdf

I have a ckeditor (http://ckeditor.com/) on my site. I would like for users to be able to push a button to generate a PDF. Currently, I have them press the print function that came with ckeditor, which brings up the print window and from most browsers they can generate a PDF. But I want to make it simplier. I know that generating PDFs from html is difficult, but are there any simple solutions to do this (generate a PDF from the html that ckeditor gives)?
I've heard of a few solutions like fpdf, dompdf and html2pdf.
You can use iText and XMLWorker to create PDF from HTML code.
public void createPDF() throws DocumentException, IOException
{
String fileName="path you want to create the document";
Document document=new Document();
PdfWriter pdfWriter=PdfWriter.getInstance(document, new FileOutputStream(fileName));
document.open();
String finall="<h1>This is a Demo</h1>";
InputStream is = new ByteArrayInputStream(finall.getBytes());
XMLWorkerHelper.getInstance().parseXHtml(pdfWriter,document, is);
document.close();
}
Here we are using XML worker so all your tags should be closed correctly. You need iText and XMLWorker JAR files.Hope this will help you.

changing style on generating pdf with Primefaces dataExporter

I'm using Primefaces dataExporter to generate pdf from a dataTable. The pdf generated has all the columns with the same width. I'm looking for a way to change the style of the table on the postProcessor/preProcessor functions. Can I use the setHtmlStyleClass method to change something before generating pdf? I tried to use it, but with no success. I think I didnt understand it correctly.
public void preProcessPDF(Object document) throws IOException, BadElementException, DocumentException {
Document pdf = (Document) document;
pdf.setHtmlStyleClass("reportClass");
...
}
If I can use that method, where can I define reportClass ? Is it a css class for the page on the browser?
If you look at whats going on in the PDFExporter.java export method, the data table in the PDF can not manipulated.
First a com.itextpdf.text.Document object is created.
Document document = new Document(PageSize.A4.rotate());
Then the preProcessor method is called passing the Document, this is before the table is added to the PDF Document.
if(preProcessor != null) {
preProcessor.invoke(facesContext.getELContext(), new Object[]{document});
}
Then the com.itextpdf.text.pdf.PdfPTable is created. The exportPDFTable method doesn't do any special formatting.
PdfPTable pdfTable = exportPDFTable(table, excludeColumns);
document.add(pdfTable);
Now the postProcess method is called and the Document is passed again. Here I would think you would be able to access and change the PdfPTable from the Document object but looking at the iText api it doesn't look like you can.
if(postProcessor != null) {
postProcessor.invoke(facesContext.getELContext(), new Object[]{document});
}
So if you want a styled PDF table your going to have to implement your own PDF export. Hopefully looking at how the PrimeFaces PDFExporter is done will help you with that.

display an (x)html document structure in a new window

For debugging purpose, I need to create an new xml document popup to display the (x)html source structure of my current document.
But the following code does not work:
var w = window.open();
w.document.open('text/xml');
w.document.write(window.document.documentElement.innerHTML);
w.document.close();
It seems that document.open() does not accept contentType anymore.
Is there any other solution ?
Just put a textarea in your existing page and copy the innerHTML into the textarea.