When attempting to create a PDF using PdfDocument from a Webview, my content is always blank. Trying a very simple example (writting a report writter) and yet the string does not show up in the PDF. The PDF is getting create, however the text is not showing up.
WebView webview;
String summary = "<html><body><h1>A very simple line of <b>text</b> </h1></body></html>";
webview.setBackgroundColor(Color.RED);
webview.loadDataWithBaseURL(null, summary,
"text/HTML", "UTF-8", null);
PdfDocument document = new PdfDocument();
int pageNumber = 1;
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(webview.getWidth(),
webview.getHeight(), pageNumber).create();
PdfDocument.Page page = document.startPage(pageInfo);
webview.draw(page.getCanvas());
document.finishPage(page);
I also attempted to place this inside a custom webview and process in public void onPageFinished(WebView view, String url), but still no string shown in PDF.
The PDF is RED.
Related
I made an app in android studio (Java) on which is possible to fill some brief data in text boxes. Also app has two buttons, one for loading image from gallery and one for saving complete data to pdf.
I can successfully save all text data, but have problem with loaded image. Image is sucessfully loaded to app, but i dont know hot to save it to pdf. Image is loaded as an ImageView object.
Just to mention for pdf part i use itext.
Pls, help with hints or code for saving ImageVIew object to pdf file.
i had the same problem, but i can fixed.
first i changed the way i used to pick the image and replaced it with this code:
Intent getImage = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(getImage, GALLERY_REQUEST_CODE);
then, in the class onActivityResult i transformed the uri to a bitmap using this:
if(requestCode==GALLERY_REQUEST_CODE && resultCode== RESULT_OK && data!=null){
Uri imageData = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(imageData,filePath,null,null,null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePath[0]);
String myPath = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(myPath);
then passed the bitmap to the pdf
pdf.addFoto(bitmap);
and finally, in the pdf template i used this to put the image in a table:
public void addFoto (Bitmap u) {
try{
PdfPTable tabla = new PdfPTable(1);
tabla.setWidthPercentage(60);
tabla.setSpacingBefore(10);
tabla.setSpacingAfter(10);
Bitmap bmp = u;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
PdfPCell imageCell = new PdfPCell();
imageCell.addElement(image);
tabla.addCell(imageCell);
document.add(tabla);
Look into PdfDocument
Example from the documentation
// create a new document
PdfDocument document = new PdfDocument();
// crate a page description
PageInfo pageInfo = new PageInfo.Builder(new Rect(0, 0, 100, 100), 1).create();
// start a page
Page page = document.startPage(pageInfo);
// draw something on the page
View content = getContentView();
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
. . .
// add more pages
. . .
// write the document content
document.writeTo(getOutputStream());
// close the document
document.close();
I am programatically loading a html code into webview of android and then programatically making a pdf file from it and saving to disk. I can successfully create the pdf but some strange headers and footer are added because of which my pdf page size increases to 2 instead of 1. How do i remove that. Below is the image. I have removed the actual data from image but you can see the headers and footers which are covering up extra space. Those headers and footers are marked in red in the below image.
code which generates pdf:
Part 1:
printWrapper.setHtmlContent(billDetails);
//webView.setBackgroundColor(Color.GREEN);
webView.loadDataWithBaseURL(null, printWrapper.getHtmlContent(), "text/HTML", "UTF-8", null);
// Keep a reference to WebView object until you pass the PrintDocumentAdapter
// to the PrintManager
mWebView = webView;
Part 2:
String jobName = getString(R.string.app_name) + " Document";
PrintAttributes attributes = new PrintAttributes.Builder()
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.setResolution(new PrintAttributes.Resolution("pdf", "pdf", 600, 600))
.setMinMargins(PrintAttributes.Margins.NO_MARGINS).build();
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/");
PdfPrint pdfPrint = new PdfPrint(attributes);
pdfPrint.print(webView.createPrintDocumentAdapter(jobName), path, "Invoice.pdf");
Can anyone help me here to solve this?
I am designing a speech-to-text android application where I display the converted text in a textview. I want to save the converted text in the form of a word file/pdf on the user's device. Can somebody tell me how to do it. Thanks.
To create a .pdf file
// create a new document
PdfDocument document = new PdfDocument();
// crate a page description
PageInfo pageInfo = new PageInfo.Builder(new Rect(0, 0, 100, 100), 1).create();
// start a page
Page page = document.startPage(pageInfo);
// draw something on the page
View content = getContentView();
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
. . .
// add more pages
. . .
// write the document content
document.writeTo(getOutputStream());
// close the document
document.close();
for more detail please have a look on below link:
https://developer.android.com/reference/android/graphics/pdf/PdfDocument.html
I hope, it will work.
I have a 300 page PDF file. I want to create a new PDF file from my chosen pages the existing pdf file.
I have created a blank PDF File (using PDFBox ) like this:
// Create a new empty document
PDDocument document = new PDDocument();
// Create a new blank page and add it to the document
PDPage blankPage = new PDPage();
document.addPage( blankPage );
// Save the newly created document
document.save("BlankPage.pdf");
and this is how I am reading a page from pdf File.
PDDocument doc = PDDocument.load("Hello World.pdf");
PDPage firstPage = (PDPage) doc.getDocumentCatalog().getAllPages().get(67);
My question is, how do I get the contents of 'firstpage' into "blankPage.pdf".
If I can choose the x,y coordinates of the firstpage(where it is to be overlayed), it would be even better.
P.S.
The page sizes of my Hello World file are not A4.Each page is more like a thumbnail with text and shapes.So, overlaying is possible on A4. Also, I do not want to convert the files to image and then onverlay, I want the whole pdf file to be pasted as is(without converting to image first)
it turns out that iText can do the same thing.Here is the code for that :
PdfReader reader = new PdfReader("Hello World.pdf");
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("RESULT.PDF"));
document.open();
PdfContentByte canvas = writer.getDirectContent();
PdfImportedPage page;
for (int i = 3; i <=6; i++) {
page = writer.getImportedPage(reader, i);
canvas.addTemplate(page, 1f, 0, 0, 1, 0, i*30-250);
}
document.close();
I was trying to display a static html page in an android app having some images in it.
I saved the image in assests folder and also in res/raw folder.
I was loading html file with this technique and html file is also in res/raw folder.
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
String data = ResourceUtils.loadResToString(R.raw.temp, this);
final String mimetype = "text/html";
final String encoding = "UTF-8";
mWebView.loadData(data, mimetype, encoding);
I tried different ways to refer to image like.
img1.png, /img1.png, file:///img1.png, content://img1.png
HtmlViewer/assets/img1.png, content:///HtmlViewer/assets/img1.png and file:///HtmlViewer/assets/img1.png
PackageName/assets/img1.png, content:///PackageName/assets/img1.png and file:///PackageName/assets/img1.png
Can anyone tell me how to get path for image?
file:///android_asset/file_name.ext
This should work. Use this method instead.
loadDataWithBaseURL("file:///android_asset/", summary, "text/html", "utf-8", null);