Showing posts with label Convert link to PDF. Show all posts

Introduction In this article, we will learn how to convert any web URL into a PDF file in Android without using any third-party libr...

How to Convert Any Web URL to PDF in Android (Without Using any 3rd Party Library) How to Convert Any Web URL to PDF in Android (Without Using any 3rd Party Library)

A blog about android developement

Convert link to PDF


Introduction

In this article, we will learn how to convert any web URL into a PDF file in Android without using any third-party libraries such as iText. Although iText is widely used for PDF generation, its AGPL license may not be suitable for all applications.

Using iText Library

iText is powerful and useful, but its AGPL licensing requires your project to also be released under AGPL if you use it. This limitation makes many developers search for an alternative.

Reference: http://developers.itextpdf.com/

Alternative Commercial Solution for iText Library

If you need a commercial library without AGPL restrictions, IronPDF is a great alternative. It supports full HTML/CSS rendering and works on Java server environments. Since it doesn’t run directly on Android devices, your Android app can call a backend API to generate PDFs.

// Server-side Java code example using IronPDF
import com.ironsoftware.ironpdf.*;
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.androidmads.info/");
pdf.saveAs("website.pdf");

This approach offloads heavy processing to the server. IronPDF supports Java, .NET, Node.js, and Python. If you need PDF generation fully inside Android without servers, the Android Print API is the best solution.

Google Print API Reference: https://developer.android.com/training/printing/

Coding Overview

We will break the implementation into the following steps:

  1. Create a new Android project.
  2. Set up Print Adapter Extension.
  3. Implement URL-to-PDF file generation.

Step 1: Creating a New Android Project

  1. Open Android Studio and create a new project.
  2. Name the project and select an Empty Activity.

Step 2: Setting Up the Print Adapter Extension

  1. To generate a PDF we must use PrintDocumentAdapter.LayoutResultCallback.
  2. Create a new Java file inside android.print package named PdfPrint.java.
  3. Paste the following code:
@SuppressWarnings("ALL")
public class PdfPrint {
...
}

Step 3: Converting Web URL to PDF

1. Add WebView in activity_main.xml

<RelativeLayout ... >
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/textView"
android:text="Saving..."
android:visibility="gone" />
</RelativeLayout>

2. Load WebView in MainActivity

webView = findViewById(R.id.webView);
webView.loadUrl("https://www.androidmads.info/");
webView.setWebViewClient(new WebViewClient());

3. Create Print Adapter

PrintAttributes printAttributes = new PrintAttributes.Builder()
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.setResolution(new PrintAttributes.Resolution("pdf", "pdf", 600, 600))
.setMinMargins(PrintAttributes.Margins.NO_MARGINS)
.build();

4. Generate PDF

new PdfPrint(printAttributes).print(
printAdapter,
file,
fileName,
new PdfPrint.CallbackPrint() {
@Override
public void onSuccess(String path) { }
@Override
public void onFailure(Exception ex) { }
});

Full MainActivity.java Code

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class MainActivity extends AppCompatActivity {
...
}

Download Full Source Code

You can download the complete project from GitHub: