I am trying to read a pdf from a url without downloading and then i am trying to flatten it .
This is the code :
import java.io.FileOutputStream;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class FormFillAndMakeItFlattenPDF {
public static void main(String[] args) {
try {
PdfReader reader = new PdfReader("http://www.irs.gov/pub/irs-pdf/fw4.pdf");
PdfStamper stamp2 = new PdfStamper(reader, new FileOutputStream("C:\\Flattened.pdf"));
AcroFields form2 = stamp2.getAcroFields();
stamp2.setFormFlattening(true);
stamp2.close();
}
catch (Exception de) {
de.printStackTrace();
}
}
}
However, this is throwing an error/exception . This is the stacktrace :
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:352)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:214)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:201)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:377)
at java.net.Socket.connect(Socket.java:530)
at java.net.Socket.connect(Socket.java:480)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:406)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:541)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:245)
at sun.net.www.http.HttpClient.New(HttpClient.java:318)
at sun.net.www.http.HttpClient.New(HttpClient.java:335)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:832)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:773)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:698)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1021)
at java.net.URL.openStream(URL.java:1009)
at com.lowagie.text.pdf.RandomAccessFileOrArray.<init>(Unknown Source)
at com.lowagie.text.pdf.RandomAccessFileOrArray.<init>(Unknown Source)
at com.lowagie.text.pdf.PRTokeniser.<init>(Unknown Source)
at com.lowagie.text.pdf.PdfReader.<init>(Unknown Source)
at com.lowagie.text.pdf.PdfReader.<init>(Unknown Source)
at FormFillAndMakeItFlattenPDF.main(FormFillAndMakeItFlattenPDF.java:18)
Can anybody tell me , what i am doing wrong here ?
If the problem happens because of proxy, call this at application startup.
System.setProperty("http.proxyHost", Config.PROXY_HOST);
System.setProperty("http.proxyPort", Config.PROXY_PORT);
where you change Config.X to your system values.
Note: for https you need to add similar lines with https.proxyHost and https.proxyPort
Your exact code works for me. I am using iText-4.2.0 (*) on Windows with Java 7.
Here is the code I ran, the test is green and "Flattened.pdf" is created correctly:
public class PdfTest {
#Test
public void testFlatten() throws Exception {
PdfReader reader = new PdfReader("http://www.irs.gov/pub/irs-pdf/fw4.pdf");
PdfStamper stamp2 = new PdfStamper(reader, new FileOutputStream("C:\\Users\\david\\Flattened.pdf"));
AcroFields form2 = stamp2.getAcroFields();
stamp2.setFormFlattening(true);
stamp2.close();
}
}
(*) https://github.com/ymasory/iText-4.2.0
Related
I have a .jar file named "DynamicContentLoader.jar" that executes a Java process that connects to a web page, using HtmlUnit, and prints its Html document via System.out.println();. This Process takes one argument from the command line: the URI of the webpage needed to be retrieved.
Code of the Java process thats exported to the .jar file:
import java.io.IOException;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class DynamicContentLoader {
public static void main(String[] args) {
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);
String s = DynamicContentLoader.loadHtml("https://query.nytimes.com/search/sitesearch/?action=click&contentCollection®ion=TopBar&WT.nav=searchWidget&module=SearchSubmit&pgtype=Homepage#/Donald%20Trump");
System.out.println(s);
}
public static String loadHtml(String url) {
final WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setCssEnabled(false); //if you don't need css
webClient.getOptions().setThrowExceptionOnScriptError(false); // stop process breaking exception throws
HtmlPage page;
try {
page = webClient.getPage(url);
webClient.waitForBackgroundJavaScript(20 * 1000); /* will wait JavaScript to execute up to 5s */
String pageAsXml = page.asXml();
webClient.close();
return pageAsXml;
} catch (FailingHttpStatusCodeException | IOException e) {
return null;
}
}
}
Next, I execute this .jar within a Mono project with a class that uses a Process Object to execute the .jar, read its StandardOutput stream into a StringBuilder, then create and return an HtmlAgilityPack.HtmlDocument object from StringBuilder.ToString();:
using System;
using System.Diagnostics;
using System.Text;
using HtmlAgilityPack;
namespace Search {
public static class DynamicContentLoader {
// path of .jar file in ProjectDirectory/Resources/.jar
readonly static string jarPath =
AppDomain.CurrentDomain.BaseDirectory +
"Resources/DynamicContentLoader.jar";
public static HtmlDocument LoadDynamicWebPage(string url) {
var startInfo = new ProcessStartInfo("java", #" -jar "
+ jarPath + " \'" + url + "\'");
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
var javaProcess = new Process();
javaProcess.StartInfo = startInfo;
javaProcess.Start();
var output = new StringBuilder();
while (!javaProcess.HasExited) {
output.Append(javaProcess.StandardOutput.ReadToEnd());
}
if (output.Length > 0) {
var doc = new HtmlDocument();
doc.LoadHtml(output.ToString());
// looking see if correct Html doc
Console.WriteLine(doc.DocumentNode.InnerHtml);
return doc;
}
return null;
}
}
}
My issue is that when I run the .jar from the command line,
"java -jar path/to/file/DynamicContentLoader.jar 'some uri'"
I get the correctly loaded Html doc/string. However, my C# code above returns a different, incomplete Html doc/string, or even crashes with exceptions like:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
atorg.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: com.gargoylesoftware.htmlunit.ScriptException: TypeError:
Cannot find function all in object function Promise() { [native code] }
Does anyone know what may cause the difference in behavior between these two different execution methods or what will fix this issue?
I am building a swing application to download multiple files over the internet and save to a windows fileshare. I have used SwingWroker which internally uses the ExecutorService which internally queues them and downloads 10 at a time, but for some reason after downloading say 2 - 3 MB of file it stops and moves to next downloading file, They are downloaded in a batch of 10 as SwingWorker has fixed it in number of Threads for the Executor Service.
I have to write these files in a windows file share and I am using nio.FileChannels to do that. There are files ranging from 50-60 each weighing around 300MB - 500MB. The file links are located on a webpage to where I get to by login in using credentials on a login page(with a post request) over the internet before that I specify CookieHandler.setDefault(new CookieManager()) at the beginning and so it behaves like a browser to me.
Another observation is when I download them locally (not to a windows server share) they do work fine.
This is the code I am using
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import javax.swing.SwingWorker;
public class DownloadProcess extends SwingWorker<Boolean, String> {
private String urlPath, filePath;
public DownloadProcess(String urlPath, String filePath){
this.urlPath = urlPath;
this.filePath = filePath;
}
#Override
protected Boolean doInBackground() {
boolean taskState = true;
URLConnection httpConn = null;
ReadableByteChannel readableByteChannel = null;
FileOutputStream fileOutputStream = null;
FileChannel fileOutputChannel = null;
try{
//String filePath = "\\\\fileshare.server\\xyz.txt";
//String urlPath = "http://example.com/anyBigFile.1GB.docx";
File localFile = new File(filePath);//File share
boolean itsThere = localFile!=null && localFile.exists();
long done = itsThere ? localFile.length() : 0;
URL url = new URL(urlPath);
httpConn = url.openConnection();
httpConn.setRequestProperty("Connection", "keep-alive");
if(itsThere) {
httpConn.setRequestProperty("Range","bytes="+done+"-");
}
readableByteChannel = Channels.newChannel(httpConn.getInputStream());
fileOutputStream = itsThere ? new FileOutputStream(filePath) : new FileOutputStream(filePath,true);
fileOutputChannel = fileOutputStream.getChannel();
for (long position = done, size = httpConn.getContentLength(); position < size && !isCancelled(); ) {
position += fileOutputChannel.transferFrom(readableByteChannel, position, 1 << 16);
}
//done
}catch(Exception e){
taskState = false;
e.printStackTrace();
}finally{
//close streams conns etc
}
return taskState;
}
}
This is the error stack trace that I get after 5 - 10 mins of download
/*
javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLException: java.net.SocketException: Connection reset
at sun.security.ssl.SSLSocketImpl.checkEOF(Unknown Source)
at sun.security.ssl.AppInputStream.read(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.net.www.MeteredStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
at java.nio.channels.Channels$ReadableByteChannelImpl.read(Unknown Source)
at com.objects.DownloadByteChannel.read(DownloadByteChannel.java:117)
at sun.nio.ch.FileChannelImpl.transferFromArbitraryChannel(Unknown Source)
at sun.nio.ch.FileChannelImpl.transferFrom(Unknown Source)
at com.core.DownloadTask.doInBackground(DownloadTask.java:154)
at com.core.DownloadTask.doInBackground(DownloadTask.java:59)
at com.util.ZSwingWorker$1.call(ZSwingWorker.java:286)
at java.util.concurrent.FutureTask.run(Unknown Source)
at com.util.ZSwingWorker.run(ZSwingWorker.java:325)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.net.ssl.SSLException: java.net.SocketException: Connection reset
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
at sun.security.ssl.SSLSocketImpl.handleException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.handleException(Unknown Source)
... 18 more
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.security.ssl.InputRecord.readFully(Unknown Source)
at sun.security.ssl.InputRecord.read(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
... 18 more
*/
Usage:
public static void main(String[] args){
int counter = 1;
for(String url: urls){
new DownloadProcess(url,"\\\\fileshare.server\\xyz"+(counter++)+".txt").execute();
}
}
You are going to have to change your connection timeout serverside. I picked up a few links along the way if they are of any importance:
Modify Session Security settings
Lengthening salesforce session timeout
Hope this helps, good luck and let me know :)
Connection Reset means the remote side is closing the connection with a TCP RST (reset) packet. You need to find out what the remote side isn't liking and fix it.
If the remote side is Apache maybe you are running into the KeepAliveTimeout value. By default that is 5 seconds. It really sounds like you are running into some sort of configured limit on the remote side. When that happens the server is kicking you off with a reset.
I am working a on project which should render SSRS reports in Java.
Below is the procedure that I have followed.
Installed Eclipse
Created a java project By setting Execution Environment Java SE 1.8
Created a proxy using wsimport
wsimport image
Added the references for the generated proxy
added classed
Written this code
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Holder;
import com.microsoft.schemas.sqlserver._2005._06._30.reporting.reportingservices.ArrayOfString;
import com.microsoft.schemas.sqlserver._2005._06._30.reporting.reportingservices.ArrayOfWarning;
import com.microsoft.schemas.sqlserver._2005._06._30.reporting.reportingservices.ExecutionHeader;
import com.microsoft.schemas.sqlserver._2005._06._30.reporting.reportingservices.ExecutionInfo;
import com.microsoft.schemas.sqlserver._2005._06._30.reporting.reportingservices.ReportExecutionService;
import com.microsoft.schemas.sqlserver._2005._06._30.reporting.reportingservices.ReportExecutionServiceSoap;
public class Render {
static {
java.net.Authenticator.setDefault(new java.net.Authenticator() {
#Override
protected java.net.PasswordAuthentication getPasswordAuthentication() {
return new java.net.PasswordAuthentication("username", "pwd".toCharArray());
}
});
}
public static void main(String[] args) {
String reportPath = "/Reporting/Customers" ;
String format = "HTML4.0";
String historyID = null;
String devInfo = "<DeviceInfo><Toolbar>False</Toolbar><HTMLFragment>True</HTMLFragment></DeviceInfo>";
String executionID = null;
Holder<String> extension = null;
Holder<String> mimeType = null;
Holder<String> encoding = null;
Holder<ArrayOfWarning> warnings = null;
Holder<ArrayOfString> streamIDs = null;
Holder<byte[]> result = new Holder<byte[]>();
ReportExecutionService res = new ReportExecutionService();
ReportExecutionServiceSoap ress = res.getReportExecutionServiceSoap();
BindingProvider bp = (BindingProvider)ress;
bp.getRequestContext().put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
ExecutionInfo execInfo = new ExecutionInfo();
execInfo = ress.loadReport(reportPath, historyID);
executionID = execInfo.getExecutionID();
bp.getRequestContext().put("sessionID", executionID);
ExecutionHeader eh = new ExecutionHeader();
eh.setExecutionID(executionID);
System.out.println(executionID);
ress.render(format, devInfo, result, extension, mimeType, encoding, warnings, streamIDs);
String resultString = new String(result.value);
}
}
When I am running this code , i am getting the below error.
Exception in thread "main" com.sun.xml.internal.ws.fault.ServerSOAPFaultException: Client received SOAP Fault from server: The session identifier is missing. A session identifier is required for this operation. ---> Microsoft.ReportingServices.Diagnostics.Utilities.MissingSessionIdException: The session identifier is missing. A session identifier is required for this operation. Please see the server log to find more detail regarding exact cause of the failure.
at com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(Unknown Source)
at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(Unknown Source)
at com.sun.xml.internal.ws.client.sei.StubHandler.readResponse(Unknown Source)
at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(Unknown Source)
at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(Unknown Source)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(Unknown Source)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(Unknown Source)
at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(Unknown Source)
at com.sun.proxy.$Proxy35.render(Unknown Source)
at Render.main(Render.java:50)
I am new to java, am I missing something ? I already set session id.
any help is appreciated.
Thanks in advance.
I'm trying to crawl a GitHub Wiki with JGit.
When I try it with one URL, it worked perfectly fine. Then I tried it with another random URL and got an error.
Please see the extract of my code:
import java.io.File;
import java.io.IOException;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
public class Main {
// with this URL I get an error
String url = "https://github.com/radiant/radiant.wiki.git";
// this URL works
// String url = "https://github.com/WardCunningham/Smallest-Federated-Wiki.wiki.git";
public static void main(String[] args) {
Main m = new Main();
m.jgitTest();
System.out.println("Done!");
}
public void jgitTest() {
try {
File localPath = File.createTempFile("TestGitRepository", "");
localPath.delete();
Git.cloneRepository().setURI(url).setDirectory(localPath).call();
} catch (IOException | GitAPIException e) {
System.err.println("excepton: " + e.getMessage());
e.printStackTrace();
}
}
}
This is the stack trace:
Exception in thread "main" org.eclipse.jgit.dircache.InvalidPathException: Invalid path (contains separator ':'): How-To:-Create-an-Extension.textile
at org.eclipse.jgit.dircache.DirCacheCheckout.checkValidPathSegment(DirCacheCheckout.java:1243)
at org.eclipse.jgit.dircache.DirCacheCheckout.checkValidPathSegment(DirCacheCheckout.java:1225)
at org.eclipse.jgit.dircache.DirCacheCheckout.checkValidPath(DirCacheCheckout.java:1185)
at org.eclipse.jgit.dircache.DirCacheCheckout.processEntry(DirCacheCheckout.java:311)
at org.eclipse.jgit.dircache.DirCacheCheckout.prescanOneTree(DirCacheCheckout.java:290)
at org.eclipse.jgit.dircache.DirCacheCheckout.doCheckout(DirCacheCheckout.java:408)
at org.eclipse.jgit.dircache.DirCacheCheckout.checkout(DirCacheCheckout.java:393)
at org.eclipse.jgit.api.CloneCommand.checkout(CloneCommand.java:236)
at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:127)
at Main.jgitTest(Main.java:21)
at Main.main(Main.java:13)
If you visit the wiki page of the URL that doesn't work (https://github.com/radiant/radiant/wiki), you will find this page: How To: Create an Extension.
The title of this page is the cause of the error: Invalid path (contains separator ':'): How-To:-Create-an-Extension.textile.
I assume I need to escape all output.
I suppose you are on windows. You can't create a file on windows having the ":" in the name. JGit should handle it somehow, so I suppose this is a bug in JGit.
I had the same problem with pure git, and this answer helped me:
git config core.protectNTFS false
The title doesn't allow me to say Problem, so the actual error message was -
java.io.IOException: Problem reading font data.
at java.awt.Font.createFont(Unknown Source)
at AddFont.createFont(AddFont.java:11)
at MainFrame$1.run(MainFrame.java:105)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
The code is -
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
AddFont addFont = new AddFont();
addFont.createFont();
} catch (Exception e) {
e.printStackTrace();
}
createGUI();
} //public void run() Closing
});
}
and the file that I used to get the AddFont addFont-
import java.awt.Font;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
public class AddFont extends MainFrame{
public void createFont(){
Font ttfBase = null;
Font telegraficoFont = null;{
try {
InputStream myStream = new BufferedInputStream(new FileInputStream(FONT_PATH_TELEGRAFICO));
ttfBase = Font.createFont(Font.TRUETYPE_FONT, myStream);
telegraficoFont = ttfBase.deriveFont(Font.PLAIN, 24);
} catch (Exception ex) {
ex.printStackTrace();
System.err.println("Font not loaded.");
}
}
}
}
I was instructed to make a new thread because this is a separate problem from my other one.
Why am I getting this problem, and how can I fix it?
I have my TELEGRAFICO.TTF font in my imageFolder, which is really just my resources folder. I use
public static final String FONT_PATH_TELEGRAFICO = "imageFolder/TELEGRAFICO.TTF";
to call in my path.
What am I doing wrong?
EDIT - I no longer get that error message, and I don't get "Font not loaded". How can I use the font in other class files other than the one I made that method in?
(I want to use that font on buttons in multiple class files. I tried using it here -
regButton = new JButton();
regButton.setText("Foo");
regButton.setAlignmentX(Component.CENTER_ALIGNMENT);
regButton.setFont(telegraficoFont);
But it said telegraficoFont cannot be resolved to a variable. (Because it was in a different class file.)
How can I fix this? Thanks again for the help.
In some cases the cause is the running instance not being able to write to the Java temp directory (java.io.tmpdir).
If your are running it on tomcat maybe you deleted the temp directory of the tomcat installation, or the folder have wrong permissions.
(tomcat folder)/temp
As you have a problem with possible font file locating and font stream creation,
Try this >> Issue loading custom font AND http://forums.devshed.com/showpost.php?p=2268351&postcount=2
To answer your question "how to make this function easy to use everywhere", do as this:
public class AddFont extends MainFrame {
private static Font ttfBase = null;
private static Font telegraficoFont = null;
private static InputStream myStream = null;
private static final String FONT_PATH_TELEGRAFICO = "imageFolder/TELEGRAFICO.TTF";
public Font createFont() {
try {
myStream = new BufferedInputStream(
new FileInputStream(FONT_PATH_TELEGRAFICO));
ttfBase = Font.createFont(Font.TRUETYPE_FONT, myStream);
telegraficoFont = ttfBase.deriveFont(Font.PLAIN, 24);
} catch (Exception ex) {
ex.printStackTrace();
System.err.println("Font not loaded.");
}
return telegraficoFont;
}
}
And then in your calling class:
public class Test {
public static Font font = null;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
if (font == null) {
font = AddFont.createFont();
}
} catch (Exception e) {
e.printStackTrace();
}
createGUI();
} // public void run() Closing
});
}
}
In some cases, maybe the Fontconfig is lack in your running environment. After installing, everything is OK.
For example,
yum install fontconfig
you could try to install "dejavu-sans-fonts" and fontconfig, it works