Pesquisar neste blogue

Mostrar mensagens com a etiqueta stackoverflow. Mostrar todas as mensagens
Mostrar mensagens com a etiqueta stackoverflow. Mostrar todas as mensagens

segunda-feira, 17 de dezembro de 2018

CSV handling in Java

These past days i've been trying to load CSV files in Java. Used opencsv with some degree with success, however some fields were still misinterpreted .
Next an old friend by name of FasterXML/jackson came to the rescue, however i was still having some issues.
Last but not least on stackoverflow.com (where else) i came across https://simpleflatmapper.org/ and with something like:


CsvParser.separator(';')

   .mapTo(MyClass.class)

   .headers("my","headers","list")

   .iterator(new File(filepath));


I was up and running .

Note : If you find MalformedInputException check if your input is UTF-8 , more info here  http://biercoff.com/malformedinputexception-input-length-1-exception-solution-for-scala-and-java/

sexta-feira, 29 de junho de 2018

Generating PDF from a webpage using JavaScript

A requisite for a project i was working on a while back was to generate a reader like version of a webpage and export it to PDF.
I started by getting a printable format for the webpage which was not that difficult, just removing some classes and adding some css with Jquery.

Now came the fun part.

What if i could do all of this on the client side, using JavaScript.



Imagine you're the server and your project manager is the browser. What a fine day it would be when a project manager interrupts you with something he/she could do.
Moreover i would save some cpu cycles on the server and learn something new in the process.

So with the help of stackoverflow my journey began.
What do i need :

  • Something to generate PDF in JavaScript -I came across jsPDF it has a very simple API and is really simple to use.
  • Now i need something that can make like a screenshot of the element . Again with the help of StackOverflow i came across  - html2canvas .

Using both together is really simple

        html2canvas($("#canvas"), {
            onrendered: function(canvas) {         
                var imgData = canvas.toDataURL(
                    'image/png');              
                var doc = new jsPDF('p', 'mm');
                doc.addImage(imgData, 'PNG', 10, 10);
                doc.save('sample-file.pdf');
            }
        });



From post at stackoverflow.com

I do recommend taking a look at the of jsPDF Examples
If your files are coming out a bit on the large size you may also compress it -jsPDF constructor has a flag to enable compression. My pdf came from about 14 megs to 240kb.




 The trickiest bit happens when your image is bigger than a page. In that case you must treat your image like a sliding window, effective splitting the image through several pages. A good example code can be found on

Hopefully you find this useful, i have tried to gather most of the sources i went through to learn.
Credit to the devs @ stackoverflow .


And thanks to those that :





quinta-feira, 5 de abril de 2018

How to convert Pem certificate to Hex string in Java (a stackoverflow conglumerate)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//*******************************************************************
// Welcome to CompileJava!
// If you experience any issues, please contact us ('More Info')  -->
//*******************************************************************

import java.lang.Math; // headers MUST be above the first class
import java.util.Base64;

// one class needs to have a main() method
public class HelloWorld
{
  public static String toHexString( byte[] bytes )
  {
      StringBuffer sb = new StringBuffer( bytes.length*2 );
      for( int i = 0; i < bytes.length; i++ )
      {
          sb.append( toHex(bytes[i] >> 4) );
          sb.append( toHex(bytes[i]) );
      }

      return sb.toString();
  }
  private static char toHex(int nibble)
  {
      final char[] hexDigit =
      {
          '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
      };
      return hexDigit[nibble & 0xF];
  }
  // arguments are passed using the text field below this editor
  public static void main(String[] args)
  {
    String pemText="-----BEGIN CERTIFICATE-----"+
"blablabla"+
"-----END CERTIFICATE-----";
   byte[] certificateBytes = Base64.getDecoder().decode(
    pemText.replaceAll("-----(BEGIN|END) CERTIFICATE-----", "").replaceAll("\n", "").getBytes()
);
    System.out.println(toHexString(certificateBytes));
  }
}