itext could not parse bootstrap.css - java

code:
package test.test1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.Pipeline;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.css.CssFile;
import com.itextpdf.tool.xml.css.StyleAttrCSSResolver;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
public class Table {
File oFile = new File("c:\\test\\1.pdf");
public static Document document = new Document(PageSize.A4, 0, 0, 0, 0);
PdfWriter writer =null;
public static void main(String[] args) throws IOException, DocumentException {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:\\test\\1.pdf"));
document.open();
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
CSSResolver cssResolver = new StyleAttrCSSResolver();
InputStream cssIS = Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream("css\\index.css");
CssFile cssfile = XMLWorkerHelper.getCSS(cssIS);
InputStream cssIS1 = Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream("css\\bootstrap.css");
CssFile cssfile1 = XMLWorkerHelper.getCSS(cssIS1);
cssResolver.addCss(cssfile);
cssResolver.addCss(cssfile1);
Pipeline<?> pipeline =
new CssResolverPipeline(cssResolver,
new HtmlPipeline(htmlContext,
new PdfWriterPipeline(document, writer)));
XMLWorkerHelper.getInstance().parseXHtml(writer, document,new FileInputStream("resources\\index.html"),
new FileInputStream("resources\\css\\bootstrap.css"));
document.close();
System.out.println( "PDF Created!" );
}
}
Description:
I am trying to generate html to pdf file using itext library in java.
Input: index.html, bootstrap.css,index.css
output: pdf file
Exception:
Exception in thread "main" java.lang.NumberFormatException: For input string: "100%"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222)
at java.lang.Float.parseFloat(Float.java:422)
at com.itextpdf.tool.xml.css.FontSizeTranslator.getFontSize(FontSizeTranslator.java:186)
at com.itextpdf.tool.xml.css.FontSizeTranslator.translateFontSize(FontSizeTranslator.java:165)
at com.itextpdf.tool.xml.html.AbstractTagProcessor.startElement(AbstractTagProcessor.java:120)
at com.itextpdf.tool.xml.pipeline.html.HtmlPipeline.open(HtmlPipeline.java:105)
at com.itextpdf.tool.xml.XMLWorker.startElement(XMLWorker.java:103)
at com.itextpdf.tool.xml.parser.XMLParser.startElement(XMLParser.java:372)
at com.itextpdf.tool.xml.parser.state.TagEncounteredState.process(TagEncounteredState.java:104)
at com.itextpdf.tool.xml.parser.XMLParser.parseWithReader(XMLParser.java:237)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:215)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:176)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:222)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:226)
at test.test1.Table.main(Table.java:160)
so here i think font getting problem here,please tell me what exactly i do?

Most of the CSS is not supported by itext's XML worker.
Since bootstrap contains very advanced CSS, this is pretty much doomed...
You'll need to write your own CSS file with very limited styling compared to bootstrap
Edit:
Updated link to supported css:
https://wiki.base22.com/btg/itext-5-css-properties-support-when-using-html-to-pdf-features-147327746.html

Related

How to create pptx file for Link-Video in Slide using Apache-POI

I want to create pptx file having linked-Video in slides using Apache-poi.
I got one example in Apache-Examples code
poi-4.1.2\src\scratchpad\testcases\org\apache\poi\hslf\model\TestMovieShape.
Using this example I can able to create .ppt file but it's not creating .pptx file.
Also using this example media-controls are not visible.
Only a few lines needed to be changed opposed to the embedded video case.
The video URI is not a real URI, but simply a relative .mp4 filename in the same directory. Although I haven't tested it, absolute file URIs should also work.
I haven't implemented the frame extraction, as it's mentioned in the embedded example - so either look for an archived version of xuggler or find a different library to extract the preview image.
Tested with Powerpoint 2016 / POI 5.0.0-Snapshot.
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.xmlbeans.XmlCursor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTHyperlink;
import org.openxmlformats.schemas.presentationml.x2006.main.*;
import javax.xml.namespace.QName;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import static org.apache.poi.openxml4j.opc.PackageRelationshipTypes.CORE_PROPERTIES_ECMA376_NS;
public class LinkVideoToPptx {
public static void main(String[] args) throws IOException, URISyntaxException {
XMLSlideShow pptx = new XMLSlideShow();
String videoFileName = "file_example_MP4_640_3MG.mp4";
XSLFSlide slide1 = pptx.createSlide();
PackagePart pp = slide1.getPackagePart();
URI mp4uri = new URI("./"+videoFileName);
PackageRelationship prsEmbed1 = pp.addRelationship(mp4uri, TargetMode.EXTERNAL, "http://schemas.microsoft.com/office/2007/relationships/media");
PackageRelationship prsExec1 = pp.addRelationship(mp4uri, TargetMode.EXTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/video");
File previewJpg = new File("preview.jpg");
XSLFPictureData snap = pptx.addPicture(previewJpg, PictureData.PictureType.JPEG);
XSLFPictureShape pic1 = slide1.createPicture(snap);
pic1.setAnchor(new Rectangle(100, 100, 500, 400));
CTPicture xpic1 = (CTPicture)pic1.getXmlObject();
CTHyperlink link1 = xpic1.getNvPicPr().getCNvPr().addNewHlinkClick();
link1.setId("");
link1.setAction("ppaction://media");
CTApplicationNonVisualDrawingProps nvPr = xpic1.getNvPicPr().getNvPr();
nvPr.addNewVideoFile().setLink(prsExec1.getId());
CTExtension ext = nvPr.addNewExtLst().addNewExt();
ext.setUri("{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}");
String p14Ns = "http://schemas.microsoft.com/office/powerpoint/2010/main";
XmlCursor cur = ext.newCursor();
cur.toEndToken();
cur.beginElement(new QName(p14Ns, "media", "p14"));
cur.insertNamespace("p14", p14Ns);
cur.insertAttributeWithValue(new QName(CORE_PROPERTIES_ECMA376_NS, "link"), prsEmbed1.getId());
cur.dispose();
CTSlide xslide = slide1.getXmlObject();
CTTimeNodeList ctnl;
if (!xslide.isSetTiming()) {
CTTLCommonTimeNodeData ctn = xslide.addNewTiming().addNewTnLst().addNewPar().addNewCTn();
ctn.setDur(STTLTimeIndefinite.INDEFINITE);
ctn.setRestart(STTLTimeNodeRestartType.NEVER);
ctn.setNodeType(STTLTimeNodeType.TM_ROOT);
ctnl = ctn.addNewChildTnLst();
} else {
ctnl = xslide.getTiming().getTnLst().getParArray(0).getCTn().getChildTnLst();
}
CTTLCommonMediaNodeData cmedia = ctnl.addNewVideo().addNewCMediaNode();
cmedia.setVol(80000);
CTTLCommonTimeNodeData ctn = cmedia.addNewCTn();
ctn.setFill(STTLTimeNodeFillType.HOLD);
ctn.setDisplay(false);
ctn.addNewStCondLst().addNewCond().setDelay(STTLTimeIndefinite.INDEFINITE);
cmedia.addNewTgtEl().addNewSpTgt().setSpid(""+pic1.getShapeId());
try (FileOutputStream fos = new FileOutputStream("mp4test-poi.pptx")) {
pptx.write(fos);
}
}
}

Convert Extent Report HTML file to PDF

I am trying to convert Extent report HTML file to PDF, however i did not succeed.
Below is the code i tried.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
public class Demo
{
public static void main( String[] args ) throws DocumentException, IOException
{
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("pdf.pdf"));
document.open();
XMLWorkerHelper.getInstance().parseXHtml(writer, document,new FileInputStream("html.html"));
document.close();
System.out.println( "PDF Created!" );
}
}
Exception in thread "main" com.itextpdf.tool.xml.exceptions.RuntimeWorkerException: Invalid nested tag head found, expected closing tag link.
at com.itextpdf.tool.xml.XMLWorker.endElement(XMLWorker.java:134)
at com.itextpdf.tool.xml.parser.XMLParser.endElement(XMLParser.java:396)
at com.itextpdf.tool.xml.parser.state.ClosingTagState.process(ClosingTagState.java:70)
at com.itextpdf.tool.xml.parser.XMLParser.parseWithReader(XMLParser.java:236)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:214)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:175)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:238)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:210)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:183)
at com.tib.controlStatements.Demo.main(Demo.java:22)
HTML File Link:
https://drive.google.com/open?id=1UrHafoit0rJuhTC0QRqCe9bC5PMpqIWS
Try this example. You did not create the file and trying to fill it.
package sandbox.xmlworker;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
#WrapToTest
public class D02_ParseHtml {
public static final String HTML = "resources/xml/walden.html";
public static final String DEST = "results/xmlworker/walden1.pdf";
/**
* Html to pdf conversion example.
* #param file
* #throws IOException
* #throws DocumentException
*/
public void createPdf(String file) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
// step 3
document.open();
// step 4
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new FileInputStream(HTML));
// step 5
document.close();
}
/**
* Main method
*/
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new D02_ParseHtml().createPdf(DEST);
}
}
Finally found a work around for it. So what I have done is Added A css file with some adjustments in to the html and was able to covert to PDF and print with attached screenshots.

Importing PDF to String in java

i need to extract text from a pdf file using java. I found iText but it doesn't work the way i wanted it to. Here's my code
package com.itextpdf.mavenproject1;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfButtonFormField;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfTextAnnotation;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.canvas.parser.PdfTextExtractor;
import com.itextpdf.test.annotations.WrapToTest;
import java.io.File;
import java.io.IOException;
public class zczytywanie {
public static void main(String args[]) throws IOException {
PdfDocument pdfDoc = new PdfDocument(new PdfReader("D:/pdf/pdf"));
String page= PdfTextExtractor.getTextFromPage(pdfDoc, 1);
System.out.println(page);
}
}
And it tells me that there is an error in the line where i try to use PDdfTextExtractor (PdfDocument can not be converted to pdfPage, although i found that pdfDoc has to be PdfReader)
It doesn't work with
PdfReader pdfDoc = new PdfReader("D:/pdf/pdf");
either.
You can try PDFBox or Tikka. But here I am giving an example for PDFBox
Add the PDFBox jar dependency to your pom.xml.
<dependencies>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.23</version>
</dependency>
</dependencies>
Java class
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;
import java.io.File;
import java.io.IOException;
public class TestPDF {
public static void main(String[] args) {
try (PDDocument document = PDDocument.load(new File("/path_to_your_pdf_file"))) {
document.getClass();
if(!document.isEncrypted()){
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true);
PDFTextStripper tStripper = new PDFTextStripper();
String pdfFileInText = tStripper.getText(document);
System.out.println("Text:" + pdfFileInText);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Convert docx file to pdf in java..issue

I am developing a project which needs a docx file to be converted to pdf. I found same question already posted and used the code which was provided by "Kishan C S". It uses docx4J2.8.1
The code is working fine , pdf is generated but only problem I am facing is that the docx file contains logo.jpg (images header part) which are not converted. Only textual format is converted to pdf.
I am posting the code which I have used. Please let me know what how can I solve the problem
P.S: link I referred Convert docx file into PDF with Java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.List;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFont;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class DocxConverter {
public static void main(String[] args) throws FileNotFoundException, Docx4JException, Exception {
InputStream is = new FileInputStream(new File("D:\\Test\\C_IN0004_AppointmentLetter.docx"));
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is);
List sections = wordMLPackage.getDocumentModel().getSections();
for (int i = 0; i < sections.size(); i++) {
wordMLPackage.getDocumentModel().getSections().get(i).getPageDimensions();
}
Mapper fontMapper = new IdentityPlusMapper();
PhysicalFont font = PhysicalFonts.getPhysicalFonts().get("Comic Sans MS");//set your desired font
fontMapper.getFontMappings().put("Algerian", font);
wordMLPackage.setFontMapper(fontMapper);
PdfSettings pdfSettings = new PdfSettings();
org.docx4j.convert.out.pdf.PdfConversion conversion = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);
//To turn off logger
List<Logger> loggers = Collections.<Logger> list(LogManager.getCurrentLoggers());
loggers.add(LogManager.getRootLogger());
for (Logger logger : loggers) {
logger.setLevel(Level.OFF);
}
OutputStream out = new FileOutputStream(new File("D:\\Test\\C_IN0004_AppointmentLetter.pdf"));
conversion.output(out, pdfSettings);
System.out.println("DONE!!");
}
}

How to invoke JavaScript function on pressing Enter key in PDF generated using PDFBOX?

I want to perform some action on pressing Enter Key in Textbox of PDF generated using PDFBOX library version 1.8.10.
I tried below code but it is working when field looses focus from text box.
If I enter some value in textbox and just press enter then javascript action is not invoked.
Please provide feedback how to achieve this.
import java.io.IOException;
import javax.swing.text.BadLocationException;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSFloat;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSString;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.interactive.action.PDAnnotationAdditionalActions;
import org.apache.pdfbox.pdmodel.interactive.action.type.PDActionJavaScript;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDTextbox;
import org.junit.Test;
public class TestPdfbox {
#Test
public void Sample1 () throws IOException, COSVisitorException, BadLocationException {
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
COSDictionary acroFormDict = new COSDictionary();
acroFormDict.setBoolean(COSName.getPDFName("NeedAppearances"), true);
acroFormDict.setItem(COSName.getPDFName("Fields"), new COSArray());
PDAcroForm acroForm = new PDAcroForm(doc, acroFormDict);
doc.getDocumentCatalog().setAcroForm(acroForm);
PDActionJavaScript javascript = new PDActionJavaScript("function validate(index){ app.alert(index); }");
doc.getDocumentCatalog().setOpenAction( javascript );
COSDictionary cosDict1 = new COSDictionary();
COSArray rect1 = new COSArray();
rect1.add(new COSFloat(100));
rect1.add(new COSFloat(700));
rect1.add(new COSFloat(200));
rect1.add(new COSFloat(750));
cosDict1.setItem(COSName.RECT, rect1);
cosDict1.setItem(COSName.FT, COSName.getPDFName("Tx")); // Field Type
cosDict1.setItem(COSName.TYPE, COSName.ANNOT);
cosDict1.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
cosDict1.setItem(COSName.T, new COSString("tx1"));
cosDict1.setItem(COSName.DA, new COSString("/Helv 7 Tf 0 g"));
cosDict1.setItem(COSName.V, new COSString("Test Value"));
PDTextbox textbox = new PDTextbox(doc.getDocumentCatalog().getAcroForm(), cosDict1);
// textbox.setFieldFlags(flag);
PDActionJavaScript textBoxJs = new PDActionJavaScript("validate("+1+");");
PDAnnotationAdditionalActions action = new PDAnnotationAdditionalActions();
action.setBl(textBoxJs);
textbox.getWidget().setActions(action);
page.getAnnotations().add(textbox.getWidget());
acroForm.getFields().add(textbox);
doc.save("\\path");
doc.close();
}
}

Categories

Resources