i want to convert my png image file into svg and store in file system, i not found any apropriate answer if anyone have the solution please share.
i use below code but not work
SVGTranscoder t = new SVGTranscoder();
t.addTranscodingHint(SVGTranscoder.KEY_FORMAT, true);
String svgURI = new File(inputFilePath).toURL().toString();
InputStream inputStream = new FileInputStream(inputFilePath);
Reader inputStreamReader = new InputStreamReader(inputStream);
TranscoderInput input = new TranscoderInput(inputStreamReader);
OutputStream ostream = new FileOutputStream(outputFilePath);
Writer outputStreamWriter = new OutputStreamWriter(ostream);
TranscoderOutput output = new TranscoderOutput(outputStreamWriter);
t.transcode(input, output);
ostream.flush();
ostream.close();
System.exit(0);
Try this:
Converter converter = new Converter("input.png");
// Prepare conversion options for target format SVG
ConvertOptions convertOptions = new FileType().fromExtension("svg").getConvertOptions();
// Convert to SVG format
converter.convert("output.svg", convertOptions);
Related
Below is my code to create PDF from input String or byte[]
This input is working with iText5. When I pass input to PdfReader in iText5 it was able to create PdfReader object.
Case 1:
byte[] bytesDecoded = Base64.decodeBase64(input.getBytes(StandardCharsets.UTF_8));
InputStream is = IOUtils.toInputStream(bytesDecoded.toString(), StandardCharsets.UTF_8);
PdfReader reader = new PdfReader(is);
Case 2:
PdfReader reader = new PdfReader(IOUtils.toInputStream(input, StandardCharsets.UTF_16));
Exception:
Exception in thread "main" com.itextpdf.io.IOException: PDF header not found.
at com.itextpdf.io.source.PdfTokenizer.getHeaderOffset(PdfTokenizer.java:223)
at com.itextpdf.kernel.pdf.PdfReader.getOffsetTokeniser(PdfReader.java:1018)
Not working in iText7
This is the way I have done. Sharing it as may be useful in future.
ByteArrayOutputStream pdfos = new ByteArrayOutputStream();
byte[] bytesDecoded = Base64.decodeBase64(src.getBytes(StandardCharsets.UTF_8));
ByteArrayInputStream inStream = new ByteArrayInputStream(bytesDecoded);
PdfReader reader = new PdfReader(inStream);
PdfSigner signer = new PdfSigner(reader, pdfos, false);
This is the easiest method to create a PdfReader from a Byte[] or a String.
byte[] template;
PdfReader templatePdf = new PdfReader(new RandomAccessSourceFactory().CreateSource(new MemoryStream(template).ToArray()),new ReaderProperties());
I want to extract PDF context into a String. I've done that previously using PDFBox but it doesn't support a lot of fonts I have in my PDFs.
Decided to use iText instead. How can I do that using getByteStream from a blob rather than a file on disk?
Blob blobPdf = ...;
File outputFile = new File("/tmp/blah/whatever.pdf");
FileOutputStream fout = new FileOutputStream(outputFile);
IOUtils.copy(blobPdf.getBinaryStream(), fout);
I want this sort of logic but insert the context into the String variable instead. How can I do that?
#EDIT
This is my attempt
InputStream is = resultSet.getBinaryStream(3);
PdfReader reader = new PdfReader(is);
String text = PdfTextExtractor.getTextFromPage(reader, 1);
System.out.println(text);
HI I am working on the following snippet which is supposed to convert my png file to tiff.
String fileName = "4848970_1";
// String fileName = "color";
String inFileType = ".PNG";
String outFileType = ".TIFF";
File fInputFile = new File("C:\\Users\\abc\\Downloads\\image2.png");
InputStream fis = new BufferedInputStream(new FileInputStream(fInputFile));
ImageReaderSpi spi = new PNMImageReaderSpi();
ImageReader reader = spi.createReaderInstance();
ImageInputStream iis = ImageIO.createImageInputStream(fis);
reader.setInput(iis, true);
BufferedImage bi = reader.read(0);
int[] xi = bi.getSampleModel().getSampleSize();
for (int i : xi) {
System.out.println("bitsize " + i);
}
ImageWriterSpi tiffspi = new TIFFImageWriterSpi();
TIFFImageWriter writer = (TIFFImageWriter) tiffspi.createWriterInstance();
// TIFFImageWriteParam param = (TIFFImageWriteParam) writer.getDefaultWriteParam();
TIFFImageWriteParam param = new TIFFImageWriteParam(Locale.US);
String[] strings = param.getCompressionTypes();
for (String string : strings) {
System.out.println(string);
}
//param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
//param.setCompressionType("LZW");
File fOutputFile = new File("C:\\Users\\abc\\Downloads\\" + fileName + outFileType);
OutputStream fos = new BufferedOutputStream(new FileOutputStream(fOutputFile));
ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
writer.setOutput(ios);
writer.write(null, new IIOImage(bi, null, null), param);
ios.flush();
writer.dispose();
ios.close();
But this gives me following error
Exception in thread "main" java.lang.RuntimeException: What in the stream isn't a PNM image.
at com.sun.media.imageioimpl.plugins.pnm.PNMImageReader.readHeader(PNMImageReader.java:187)
at com.sun.media.imageioimpl.plugins.pnm.PNMImageReader.read(PNMImageReader.java:301)
at javax.imageio.ImageReader.read(Unknown Source)
at com.imageconv.TiffImage.main(TiffImage.java:40)
Is it that its unable to read the PNG file or it recognises it as a non png file.Am I wrong anywhere?
You are trying to read a PNG image as if it was a PNM image. These two file formats have nothing in common; hence the error.
I want to convert my XHTML text to a PDF. I converted it to FileOutputStream but I ca'nt find a way to pass it as an input to the ITextRenderer. Is that possible, and how?
the code :
String finalXhtml=xhtmlparser(xmlText);
ByteArrayInputStream finalXhtmlStream = new ByteArrayInputStream(finalXhtml.getBytes());
String HTML_TO_PDF = "ConvertedFile.pdf";
OutputStream os = new FileOutputStream(HTML_TO_PDF);
ITextRenderer renderer = new ITextRenderer();
// renderer.loadDocument(finalXhtmlStream); i can pass a file here can i pass an input or output stream ?
renderer.layout();
renderer.createPDF(os) ;
os.close();
System.out.println("done.");
note: I can pass a file to the ITextRenderer as following:
String File_To_Convert = "report.xhtml";
String url = new File(File_To_Convert).toURI().toURL().toString();
String HTML_TO_PDF = "ConvertedFile.pdf";
OutputStream os = new FileOutputStream(HTML_TO_PDF);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
System.out.println("done.");
please let me know if I have to provide more details.
I am using following code to export HTML data to PDF with following code:
renderer.setDocumentFromString(htmls.toString());
renderer.layout();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".pdf\"");
renderer.createPDF(outputStream);
renderer.createPDF(fos);
Now here I am using inline CSS to generate PDF using style but is there any option that I can use setDocumentFromString() function by loading external CSS.
I am assuming you are using Flying Saucer. ITextRenderer has a method that does something similar:
public void setDocumentFromString(String content) {
InputSource is = new InputSource(new BufferedReader(new StringReader(content)));
Document dom = XMLResource.load(is).getDocument();
setDocument(dom, null);
}
Adapting your code, what you'd want would look something like this:
String finalXhtml=xhtmlparser(xmlText);
ByteArrayInputStream finalXhtmlStream = new ByteArrayInputStream(finalXhtml.getBytes());
String HTML_TO_PDF = "ConvertedFile.pdf";
OutputStream os = new FileOutputStream(HTML_TO_PDF);
Document document = XMLResource.load(finalXhtmlStream).getDocument();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(document, null);
renderer.layout();
renderer.createPDF(os) ;
os.close();
of course you could also do this and skip the inputstream all together:
renderer.setDocumentFromString(finalXhtml);
How do I read an image into a base64 encoded string by its ImageReader?
Here's example source code using HtmlUnit. I want to get the base64 String of img:
WebClient wc = new WebClient();
wc.setThrowExceptionOnFailingStatusCode(false);
wc.setThrowExceptionOnScriptError(false);
HtmlPage p = wc.getPage("http://flickr.com");
HtmlImage img = (HtmlImage) p.getByXPath("//img").get(3);
System.out.println(img.getImageReader().getFormatName());
The HtmlUnit's HtmlImage#getImageReader() returns javax.imageio.ImageReader which is part of standard Java 2D API. You can get an BufferedImage out of it which you in turn can write to an OutputStream of any flavor using ImageIO#write().
The Apache Commons Codec has a Base64OutputStream which you can just decorate your OutputStream with.
HtmlImage img = (HtmlImage) p.getByXPath("//img").get(3);
ImageReader imageReader = img.getImageReader();
BufferedImage bufferedImage = imageReader.read(0);
String formatName = imageReader.getFormatName();
ByteArrayOutputStream byteaOutput = new ByteArrayOutputStream();
Base64OutputStream base64Output = new base64OutputStream(byteaOutput);
ImageIO.write(bufferedImage, formatName, base64output);
String base64 = new String(byteaOutput.toByteArray());
Or if you want to write it to file directly:
// ...
FileOutputStream fileOutput = new FileOutputStream("/base64.txt");
Base64OutputStream base64Output = new base64OutputStream(fileOutput);
ImageIO.write(bufferedImage, formatName, base64output);
I'm not quite sure what exactly you want.
But what about creating your own Reader (see javax.imageio.stream.ImageInputStreamImpl), containing the Base64-stuff?
Maybe this external free Base64Encoder can help you out.
Something that could be used like this in the end?
WebClient wc = new WebClient();
wc.setThrowExceptionOnFailingStatusCode(false);
wc.setThrowExceptionOnScriptError(false);
HtmlPage p = wc.getPage("http://flickr.com");
HtmlImage img = (HtmlImage) p.getByXPath("//img").get(3);
MyBase64EncodingReader reader = new MyBase64EncodingReader(img);
System.out.println(reader.toString());
You could use one of the encodeBase64 methods
from apache commons codec.
and create a string from the resulting byte array using the String(bytes[]) constructor.