This might be common question, but I didn't found exact solution.
I have a JavaFX application which creates PDF file (using iText 7) using data from cloud sql server.
However, this file is now saved in my specified file path.
I want my user to specify where he/she wants to save that file.
String dest = "itextData//singleLR.pdf";
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
Now here, the path dest is already specified. I want it to be specified by user.
Expected output
//File browsing code on button click
//Got String in variable user_specified_path
String dest = user_specified_path;
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
As suggested by #VGR , I used JavaFX FileChooser.
However, instead of fileChooser.showOpenDialog(scene) , I used fileChooser.showSaveDialog(scene);
showOpenDialog() is used to open existing file.
showSaveDialog() is used to create new file
So my code goes like below,
print.setOnAction(event -> {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save PDF File");
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("PDF File", "*.pdf"));
File selectedFile = fileChooser.showSaveDialog(print.getScene().getWindow());
if (selectedFile != null) {
String dest = selectedfile.getAbsolutePath();
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
//DOCUMENT WRITING CODE BEGINS
}
}
For reference -> Saving File with FileChooser in JavaFX
Related
I need to transform HTML to a doc file, the HTML is filled with custom information and the images and CSS change depending on what is request.
I'm trying to use Apache POI for this, but I'm having an error
`
org.apache.poi.xwpf.converter.core.XWPFConverterException: java.lang.IllegalStateException: Expecting one Styles document part, but found 0
at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.convert(XHTMLConverter.java:72)
at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:58)
at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:38)
at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45)
My code is this:
// Load the HTML file
//Doc file
String htmlFile = "pathToHtml/file.html";
//String htmlFile = parseHTMLTemplate(disputeLetterDetails, template, fileExtension);
//new File(htmlFile);
//File file = new FileReader(htmlFile);
Path path = Path.of(htmlFile);
OutputStream in = new FileOutputStream(htmlFile, true);
// Create a new XWPFDocument
XWPFDocument document = new XWPFDocument();
// Set up the XHTML options
XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(new File("./images/")));
options.setExtractor(new FileImageExtractor(new File("./images/")));
// Convert the HTML to XWPFDocument
XHTMLConverter.getInstance().convert(document, in, options);
// Save the document to a .doc file
FileOutputStream out = new FileOutputStream("pathToHtml/OUT_from_XHTML_TEST.docx");
document.write(out);
out.close();
`
I want to get a docx file from an HTML file with the same styles but I'm getting this error `
org.apache.poi.xwpf.converter.core.XWPFConverterException: java.lang.IllegalStateException: Expecting one Styles document part, but found 0
at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.convert(XHTMLConverter.java:72)
at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:58)
at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:38)
at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45)
`
I am merging PDF content using PDFMergerUtility in Java.
PDFMergerUtility bundle= new PDFMergerUtility();
bundle.addSource(new ByteArrayInputStream(contentService.retrieveData(transaction1);
bundle.addSource(new ByteArrayInputStream(contentService.retrieveData(transaction2);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
bundle.setDestinationStream(outputStream); // setting outputstream
bundle.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
return outputStream.toByteArray(); // returning result
But by default it is opening on the last page of PDF when I try to open the resultant PDF. I am not using PDocument for these as
these are entity based documents. Due to this unable to use this below method. Can you please suggest any approach to directly set the opened page number in PDFMergerUtility.
PDPageXYZDestination destination = new PDPageXYZDestination();
destination .setPageNumber(num);
PDActionGoTo action = new PDActionGoTo();
action.setDestination(destination );
pdf.getDocumentCatalog().setOpenAction(action);
I have a String that i want to attach either to the pdf raw code somehow or, what i'm trying here is adding the string as a text file to the pdf, this is what i've come up with but it does not work.
Document document = new Document();
String directoryPath = "/storage/emulated/0/Download";
PdfWriter.getInstance(document, new FileOutputStream(directoryPath+"/Aa.pdf"));
String data = PaintView.full_string_return();
System.out.println(data);
PdfReader reader = new PdfReader(directoryPath+"/Aa.pdf");
PdfStamper stamper = new PdfStamper(reader,new FileOutputStream(directoryPath+"/Aa.pdf") );
PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(stamper.getWriter(), null, String.format("data.txt",data),String.format("data",data).getBytes(StandardCharsets.UTF_8));
stamper.addFileAttachment(String.format("data",data),fs);
stamper.close();
I know i'm not using the pdfWriter here but i use it elsewhere, just added it in case it is messing up something.
I have a file which I am trying to read the content from.
Code tried:
String userDir = System.getProperty("user.home")+"\\Downloads";
//Loading an existing document
String s=userDir+"\\PDFStatement.pdf";
File file = new File(s);
//File file = new File(s);
System.out.println("file"+file);
PDDocument document = PDDocument.load(file);// this is where I am getting error
//Instantiate PDFTextStripper class
PDFTextStripper pdfStripper = new PDFTextStripper();
//Retrieving text from PDF document
String text = pdfStripper.getText(document);
System.out.println(text);
//Closing the document
document.close();
}
Error :
java.io.IOException: Error: Expected an integer type, actual='statement'
at org.apache.pdfbox.pdfparser.BaseParser.readInt(BaseParser.java:1622)
at org.apache.pdfbox.pdfparser.PDFParser.parseObject(PDFParser.java:531)
at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:188)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1187)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1154)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1125)
Same code works when I pass the same file path with / as
File file = new File("C:/abc/abc/abc/PDFStatement.pdf");
I am using pdfbox 1.8
Thanks.
I'm currently using iText 7 to convert HTML to PDF using URL.
The problem is that on the page that I want to convert there are external css files and images that are not coming from the same source:
For example I have an external css file whose path is baseUri1/path-to-file/file.css and I have other images that are from baseUri2/path-to-file2/img.jpg
Here's the code that I am actually using for the conversion
String ADDRESS = "https://www.monagentdevoyages.fr/product?s_pid=4964";
//** The target folder for the result. *//*
String TARGET = "target/results/ch07/";
//** The path to the resulting PDF file. *//*
String DEST = String.format("%surl2pdf_print.pdf", TARGET);
PdfWriter writer = new PdfWriter(DEST);
PdfDocument pdf = new PdfDocument(writer);
PageSize pageSize = new PageSize(850, 1700);
pdf.setDefaultPageSize(pageSize);
ConverterProperties properties = new ConverterProperties();
MediaDeviceDescription mediaDeviceDescription =
new MediaDeviceDescription(MediaType.PRINT);
mediaDeviceDescription.setWidth(pageSize.getWidth());
properties.setMediaDeviceDescription(mediaDeviceDescription);
properties.setBaseUri("https://www.monagentdevoyages.fr/");
HtmlConverter.convertToPdf(new URL(ADDRESS).openStream(), pdf, properties);
When I run this code, I get the following error :
20 --- [nio-9001-exec-9] c.i.s.r.resource.ResourceResolver : Unable to retrieve image with given base URI (https://www.monagentdevoyages.fr/) and image source path (null)
Any idea how to solve this problem please ?