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 IronPDFimport 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:
- Create a new Android project.
- Set up Print Adapter Extension.
- Implement URL-to-PDF file generation.
Step 1: Creating a New Android Project
- Open Android Studio and create a new project.
- Name the project and select an Empty Activity.
Step 2: Setting Up the Print Adapter Extension
- To generate a PDF we must use PrintDocumentAdapter.LayoutResultCallback.
- Create a new Java file inside
android.printpackage named PdfPrint.java. - Paste the following code:
@SuppressWarnings("ALL")public class PdfPrint {...}
Step 3: Converting Web URL to PDF
1. Add WebView in activity_main.xml
<RelativeLayout ... ><WebViewandroid:id="@+id/webView"android:layout_width="match_parent"android:layout_height="match_parent" /><TextViewandroid: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() {@Overridepublic void onSuccess(String path) { }@Overridepublic 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:

Follow Us
Were this world an endless plain, and by sailing eastward we could for ever reach new distances