<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: pashaliski</title>
    <description>The latest articles on DEV Community by pashaliski (@pashaliski).</description>
    <link>https://dev.to/pashaliski</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F269288%2Fee72a174-8e1c-4987-9cf1-b6322731c4b5.jpeg</url>
      <title>DEV Community: pashaliski</title>
      <link>https://dev.to/pashaliski</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pashaliski"/>
    <language>en</language>
    <item>
      <title>How to use HTML5 Geolocation API</title>
      <dc:creator>pashaliski</dc:creator>
      <pubDate>Mon, 18 Nov 2019 10:40:18 +0000</pubDate>
      <link>https://dev.to/pashaliski/how-to-use-html5-geolocation-api-5cpc</link>
      <guid>https://dev.to/pashaliski/how-to-use-html5-geolocation-api-5cpc</guid>
      <description>&lt;p&gt;Geolocation API is used to retrieve the user's location. Since this can compromise privacy, the user first needs to click 'Allow' on the particular application to use their location.&lt;/p&gt;

&lt;h2&gt;Browser Support&lt;/h2&gt;

&lt;p&gt;The HTML5 Geolocation API is supported by most of today's moderns browsers. As of Chrome 50, the application needs to be hosted through HTTPS, if the URL is not secured the Geolocation API will not work.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z6Eigsh3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/56Y314P/g1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z6Eigsh3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/56Y314P/g1.png" alt="g1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;How to use&lt;/h2&gt;

&lt;p&gt;Here is the list of what you need to do to use this API:&lt;br&gt;
-Check if Geolocation API is supported in your browser&lt;br&gt;
-If supported then run getCurrentPosition() method, if not show the message&lt;br&gt;
-If the getCurrentPosition() is successful, it returns a coordinate object &lt;br&gt;
-The showPosition() function outputs the Latitude and Longitude and calls showMap() with the parameters. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ah-46BPt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/VCsfFkh/g2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ah-46BPt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/VCsfFkh/g2.png" alt="g2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the showMap() method we call function to show users position on the map, in our case we are using OpenStreetMap to make application &lt;a href="https://www.whatcountyamiin.net/"&gt;what county am i in&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ivAJxlWt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/Qjycn0H/g3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ivAJxlWt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/Qjycn0H/g3.png" alt="g3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can use Google Maps instead. This is easy to use API to show the user's position, however, you can use some Back-End library if you run any Back-End code.&lt;/p&gt;

&lt;p&gt;Check our tutorial on how to print &lt;a href="https://dev.to/pashaliski/how-to-print-html-page-using-flying-saucer-java-g60"&gt;HTML to PDF&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>html</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to print HTML page using Flying Saucer (Java)</title>
      <dc:creator>pashaliski</dc:creator>
      <pubDate>Mon, 11 Nov 2019 15:49:09 +0000</pubDate>
      <link>https://dev.to/pashaliski/how-to-print-html-page-using-flying-saucer-java-g60</link>
      <guid>https://dev.to/pashaliski/how-to-print-html-page-using-flying-saucer-java-g60</guid>
      <description>&lt;p&gt;We will keep this tutorial short and sweet. Here you can find easy steps on how you can convert any HTML source page to PDF document, including all resource files, such as Images (PNG, JPG, SVG), styles and scripts.&lt;/p&gt;

&lt;p&gt;We will use &lt;a href="https://github.com/tuhrig/Flying_Saucer_PDF_Generation"&gt;Flying Saucer&lt;/a&gt; library that is open source. This library uses a modified and older version of Itext.&lt;/p&gt;

&lt;p&gt;The main steps here are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating CSS for print (we will not cover these steps, this is different for every platform and webpage)&lt;/li&gt;
&lt;li&gt;Creating Custom Itext Renderer (we will override standard one, a simple change to enable having PdfWriter in an earlier stage) &lt;/li&gt;
&lt;li&gt;Custom User-Agent (we will override standard one, we will give one more option to enable showing of SVG images on our generated PDF)&lt;/li&gt;
&lt;li&gt;Main Class &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Main Class&lt;/h2&gt;

&lt;p&gt;Let's start with the Main Class. In this class here we will make a call to the page to get the source code. For instance, we will use &lt;a href="https://www.where-am-i.me/"&gt;where am i&lt;/a&gt; web page, because it is simple and clean. You can get the source code in multiple ways, we recommend the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating and executing HTTP Client (do not require any library)&lt;/li&gt;
&lt;li&gt;Using Chrome Driver and Selenium (slower, but is good if you need content after JS execution), good for dynamic content&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://ibb.co/HXgMFg7"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CrbRv1G---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/0QJWDJq/dev1.png" alt="dev1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that, we need to call HtmlCleaner().celean(html) method to clean the HTML.&lt;/p&gt;

&lt;p&gt;Now we have a cleaner and prepared HTML that we need to convert into PDF documents. First of all, you need to create OutputStream with the destination of your PDF. You can use the dynamic creation of PDF, to make sure that your code will not throw any exception.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://imgbb.com/"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OqP6GxHi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/1MqCSqn/dev2.png" alt="dev2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that, you need to create custom PdfWriter. For that, you need to create Rectangle, A3 format in our case and Document (Itext). Using the rectangle and the document get the instance of the writer. OutputStream should be the PDF document.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ibb.co/mB7ZrZP"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8kcmS1q4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/kyFbVbP/dev3.png" alt="dev3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you are done with that, you need to initialize new ItextRenderer, this will be modified one, on this renderer you should pass two parameters which are PdfWriter and the Document, you will initialize this in the constructor.&lt;/p&gt;

&lt;p&gt;Next, you may or may not add ITextFontResolver, but you should add CustomUserAgent which will take care of showing our SVG images (this library does not support that by default).&lt;/p&gt;

&lt;p&gt;And for last, use renderer and call createPdf function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ibb.co/qrt1M13"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z8Z_2oAc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/XChJZJr/dev4.png" alt="dev4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Custom Itext User-Agent&lt;/h2&gt;

&lt;p&gt;This part is very important when are you generating PDF since this library does not support showing of SVG images, we are forced to do that by ourselves.&lt;/p&gt;

&lt;p&gt;First of all, you need to override getImageResource method, and inside to call our new function that will take care of showing the SVG image if an extension is .svg, otherwise call the method from the parent class.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://imgbb.com/"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---JV28f6r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/RNykLsJ/dev5.png" alt="dev5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that, you need your function where you will render the SVG image. We will use PdfWriter that we created in Main Class, if we are using different ones, our PDF will not look correct.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ibb.co/2KBV19S"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hh0oKfIs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/XJT0rQD/dev6.png" alt="dev6"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Custom Itext Renderer&lt;/h2&gt;

&lt;p&gt;And the last step is our Custom Itext Renderer, we need this because we want to pass same PdfWriter to the createPdf function and to our Custom User-Agent. What you need to do it to initialize the _writer in the constructor and to set the writer on the ITextOutputDevice.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ibb.co/GHF1Fff"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MEZaiqy8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/nPRVRqq/dev7.png" alt="dev7"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you are good to go and to create your PDF files from HTML. Here is our example.&lt;br&gt;
&lt;a href="https://ibb.co/Y31zQBW"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g_Cf4uLS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/yXZwPFR/dev8.png" alt="dev8"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>html</category>
    </item>
  </channel>
</rss>
