I'm trying to show a pdf in android app. The response I get from the server is looking like this. I convert it to input stream and pass it to pdf viewer library
%PDF-1.4 %ÓôÌá 1 0 obj << /CreationDate(D:20180403085232+02'00')
/Creator(PDFsharp 1.32.3057-g (www.pdfsharp.net)) /Producer(PDFsharp
1.32.3057-g (www.pdfsharp.net))
endobj 2 0 obj << /Type/Catalog /Pages 3 0 R
endobj 3 0 obj << /Type/Pages /Count 3 /Kids[4 0 R 8 0 R 11 0 R]
endobj 4 0 obj << /Type/Page /MediaBox[0 0 612 792] /Parent 3 0 R /Contents 5 0 R /Resources << /ProcSet
[/PDF/Text/ImageB/ImageC/ImageI] /XObject << /I0 6 0 R
/ExtGState << /GS0 7 0 R
/Group << /CS/DeviceRGB /S/Transparency /I false /K false
endobj 5 0 obj << /Length 260 /Filter/FlateDecode
stream xœíRMK1½÷Wä˜M“&Mî¢àa]oâIEDF‚¿ÞÌ̺²ø¥”4y¯—´C©#¹ŽÆЃáú!ÏDpñZ†2€š
W9#¾gNŽa“/…à4÷}©gåòŠàf§{¾.«õ–àWìÚ¡WÆT4Bµ
†¡O·e[F>g^í=|UEw‡ª‚Ö$å(U‘ôÇ›„L¶ÜÑ™—~¤6•š'Ú9f”Pz›PŽ€¦¨"бv_gly›Û÷Mä~.î]
™Ûë؈?œå€«Ul¡À™y_6þ»Aÿ—ÿ¤|èYòÿX`ìfÈ»ï³ÉõŠ¹·ò endstream endobj 6 0
obj << /Type/XObject /Subtype/Image /Length 5097196
/Filter/FlateDecode /Width 2511 /Height 3531 /BitsPerComponent 8
/ColorSpace/DeviceRGB /Interpolate true
stream xœìÝ œ¤g]'ðïé9rÂáˆJAE%€ Š"ë‚FQ0 Ë!º» ¢‚+^ (h
This is my code:
// get the response as string
val body = response.body?.string()
// convert it to inputstream
val inputStream = ByteArrayInputStream(body.toByteArray(Charsets.UTF_8))
// load it wil pdf viewer
pdf_viewer.fromStream(inputStream).load()
Everything till the last line is ok. I got init FPDF library but there is nothing on the screen. It stays with white background.
If I open the pdf standalone I can see that everything is fine.
I'm using
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
here is the xml file
<com.github.barteksc.pdfviewer.PDFView
android:id="#+id/pdf_viewer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
EDIT:
the working solution was to get the bytes and pass them to PDFViewer
val bodyBytes: ByteArray = response.body!!.bytes()
runOnUiThread {
pdf_viewer.fromBytes(bodyBytes).enableSwipe(true) // allows to block changing pages using swipe
.swipeHorizontal(false)
.enableDoubletap(true)
.defaultPage(0)
.enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
.password(null)
.scrollHandle(null)
.enableAntialiasing(true) // improve rendering a little bit on low-res screens
// spacing between pages in dp. To define spacing color, set view background
.spacing(0)
.invalidPageColor(Color.WHITE) // color of page that is invalid and cannot be loaded
.load()
}
I get a pca-model
> library(sparklyr)
> library(dplyr)
> sc <- spark_connect("local", version="2.0.0")
> iris_tbl <- copy_to(sc, iris, "iris", overwrite = TRUE)
The following columns have been renamed:
- 'Sepal.Length' => 'Sepal_Length' (#1)
- 'Sepal.Width' => 'Sepal_Width' (#2)
- 'Petal.Length' => 'Petal_Length' (#3)
- 'Petal.Width' => 'Petal_Width' (#4)
> pca_model <- tbl(sc, "iris") %>%
+ select(-Species) %>%
+ ml_pca()
> print(pca_model)
Explained variance:
PC1 PC2 PC3 PC4
0.924618723 0.053066483 0.017102610 0.005212184
Rotation:
PC1 PC2 PC3 PC4
Sepal_Length -0.36138659 -0.65658877 0.58202985 0.3154872
Sepal_Width 0.08452251 -0.73016143 -0.59791083 -0.3197231
Petal_Length -0.85667061 0.17337266 -0.07623608 -0.4798390
Petal_Width -0.35828920 0.07548102 -0.54583143 0.7536574
But can`t use the resulting model to forecast.
sdf_predict(pca_model)
Source: query [?? x 6]
Database: spark connection master=local[4] app=sparklyr local=TRUE
Ends with an error
java.lang.IllegalArgumentException: requirement failed:
The columns of A don't match the number of elements of x. A: 4, x: 0
Inserting data for the forecast does not help
sdf_predict(pca_model, tbl(sc, "iris") %>% select(-Species))
Source: query [?? x 5]
Database: spark connection master=local[4] app=sparklyr local=TRUE
Ends with an error
java.lang.IllegalArgumentException: requirement failed:
The columns of A don't match the number of elements of x. A: 4, x: 0
It is generally possible to use PCA to predict in spark?
Instead of sdf_predict, use sdf_project.
> pca_projected <- sdf_project(pca_model, tbl(sc, "iris") %>% select(-Species),
+ features=rownames(pca_model$components))
> pca_projected %>% collect %>% head
# A tibble: 6 x 8
Sepal_Length Sepal_Width Petal_Length Petal_Width PC1 PC2 PC3 PC4
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 5.10 3.50 1.40 0.200 -2.82 -5.65 0.660 -0.0311
2 4.90 3.00 1.40 0.200 -2.79 -5.15 0.842 0.0657
3 4.70 3.20 1.30 0.200 -2.61 -5.18 0.614 -0.0134
4 4.60 3.10 1.50 0.200 -2.76 -5.01 0.600 -0.109
5 5.00 3.60 1.40 0.200 -2.77 -5.65 0.542 -0.0946
6 5.40 3.90 1.70 0.400 -3.22 -6.07 0.463 -0.0576
I try to create mpegts presentation timestamp. It is 5 bytes length. I found solution in source code of VLC player. It's looks like this (in C lang code):
bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
bits_write( &bits, 3, i_pts >> 30 );
bits_write( &bits, 1, 0x01 ); // marker
bits_write( &bits, 15, i_pts >> 15 );
bits_write( &bits, 1, 0x01 ); // marker
bits_write( &bits, 15, i_pts );
bits_write( &bits, 1, 0x01 ); // marker
i_header_size -= 0x5;
That means I must to collect 5 bytes from 40 bits.
For example, I need to 5 bytes from 2350 decimal number. Binary view:
1001 0010 1110
After VLC manipulation I must have this binary view:
0010 000 1 000000000000000 1 000100100101110 1
Hex view:
21 00 01 12 5D
How can I do it in Java?
Also I found Java-solution on GitHub: https://github.com/taktod/myLib/blob/master/myLib.MIT/myLib.container.mpegts/src/main/java/com/ttProject/container/mpegts/field/PtsField.java
But this realization is too difficult. For a one-time operation it is necessary to create too many helper classes like Bit1, Bit2, Bit3, etc...
This is simple bit-manipulation:
int dts = 2; // must be 2 or 3
long pts = 2350; // must be less than 8,589,934,592
byte[] output = new byte[] {
(byte) (dts << 4 | pts >> 30 | 1),
(byte) (pts >> 22),
(byte) (pts >> 15 | 1),
(byte) (pts >> 7),
(byte) (pts << 1 | 1)
};
for (byte b : output)
System.out.printf("%02x ", b); // prints: 21 00 01 12 5d
Getting below Exception while trying to read byte array using iText PdfReader,
Below is my code, I'm able to open this file in Acrobat reader
PdfReader reader = new PdfReader(bFile);
Exception:
java.lang.ClassCastException: com.itextpdf.text.pdf.PdfNull cannot be cast to com.itextpdf.text.pdf.PdfDictionary
at com.itextpdf.text.pdf.PdfReader$PageRefs.iteratePages(PdfReader.java:3712)
at com.itextpdf.text.pdf.PdfReader$PageRefs.iteratePages(PdfReader.java:3743)
at com.itextpdf.text.pdf.PdfReader$PageRefs.readPages(PdfReader.java:3548)
at com.itextpdf.text.pdf.PdfReader$PageRefs.<init>(PdfReader.java:3518)
at com.itextpdf.text.pdf.PdfReader$PageRefs.<init>(PdfReader.java:3496)
at com.itextpdf.text.pdf.PdfReader.readPages(PdfReader.java:1142)
at com.itextpdf.text.pdf.PdfReader.readPdf(PdfReader.java:659)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:176)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:244)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:234)
Im using iText 5.4.4, I couldn't find much details in googling. It looks PDF has some issues, couldn't get whats the issue. Below is the excerpts from PDF
%PDF-1.5
%âãÏÓ
1 0 obj
<<
/Type /Catalog
/Lang (en-US)
/StructTreeRoot 39 0 R
/MarkInfo <<
/Marked true
>>
/Pages 187 0 R
/AcroForm 350 0 R
/OCProperties 2131 0 R
/Outlines 2531 0 R
/OpenAction <<
/Type /Action
/S /GoTo
/D [ 3 0 R /XYZ 0 792 0 ]
>>
/ViewerPreferences <<
/HideToolbar false
/HideMenubar false
/HideWindowUI false
/FitWindow false
/CenterWindow false
>>
UPDATE: After debugging I found that /Pages 187 0 R is the problem. If I change to /Pages 2 0 R then it works. Could some please help me what does that /Pages refers ?
How can I convert unix time to Hindu calendarWikipedia time and the other way round in php, Perl or Python or Java? I know I can convert to Hebrew and Jewish. But Hindu is not an option.
To be more specific, I'm talking about the Hindu lunar calendar. The following website is working and does exactly what I want: http://web.meson.org/calendars/. For example, it 'translates' 28-1-2012 (Gregorian) to 5-11-2068 (Hind. Lun.). How can I accomplish the same task? And if there are absolutely no scrips out there, how can I write it myself?
Did you check DateTime-Indic-0.1 family of modules? At least DateTime::Indic::Chandramana
seems to have a method to convert traditional date into UTC values (utc_rd_values).
UPDATE:
I suppose Calendar::Saka may be useful as well for many users (as I have known, it's the Indian national calendar), in particular, to_gregorian() and from_gregorian() methods.
For Python, use calendar2 (note: this is not the built-in calendar module).
Sample use:
>>> from calendar2 import *
>>> old_hindu_solar_from_absolute(absolute_from_gregorian(3,1,2012))
(11, 16, 5112)
>>> old_hindu_lunar_from_absolute(absolute_from_gregorian(3,1,2012))
(12, 0, 8, 5112)
Paper: Indian Calendrical Calculations
Provides Common Lisp code in the appendix.
While a Python (or other language) solution could be written according to the paper, the authors enumerate the Indian calendar rules pretty well, so it's a pretty solid paper if you're willing to consider taking the provided Common Lisp code.
Seems to be a difficult task. According to this discussion at bytes.com there is no clear way to accomplish a 100% correct conversion. But it seems that they are wrong when they assume that the Hindu calendar has only 364 days instead of 365 (or 366 in leap years).
Here you can find a good conversion table including the handling of leap years: http://hinduism.about.com/od/basics/a/monthsdayseras.htm
If it is as easy as written there you can try something like this (php code):
<?php
function convertDateToHinduDate($date) {
$beginningDayOfMonth = array(
1 => 21,
2 => 20,
3 => 22 + (dateIsLeapYear($date) ? -1 : 0), /* 21 in leap years */
4 => 21,
5 => 22,
6 => 22,
7 => 23,
8 => 23,
9 => 23,
10 => 23,
11 => 22,
12 => 22,
);
$daysOfHinduMonth = array(
1 => 30 + (dateIsLeapYear($date) ? 1 : 0), /* 31 in leap years */
2 => 31,
3 => 31,
4 => 31,
5 => 31,
6 => 31,
7 => 30,
8 => 30,
9 => 30,
10 => 30,
11 => 30,
12 => 30,
);
$day = (int) date('d', strtotime($date));
$month = (int) date('m', strtotime($date));
$year = (int) date('Y', strtotime($date));
$monthBefore = $day < $beginningDayOfMonth[$month];
$yearBefore = $month < 3 || ($month == 3 && $day < $beginningDayOfMonth[3]);
$newYear = $year + 57 + ($yearBefore ? -1 : 0);
$newMonth = $month - 2 + ($monthBefore ? -1 : 0);
if($newMonth < 1) $newMonth = 12 + $newMonth;
$newDay = $day - $beginningDayOfMonth[$month];
if($newDay < 1) $newDay = $daysOfHinduMonth[$newMonth] + $newDay;
return date("d-m-Y", mktime(11, 59, 0, $newMonth, $newDay, $newYear));
}
function dateIsLeapYear($date) {
return date('L', strtotime($date));
}
$date = date("d-m-Y", strtotime('2012-01-28'));
echo 'Date: ', $date, ' (is leap year: ', dateIsLeapYear($date) ? 'yes' : 'no', ')<br />';
echo 'Converted Hindu date: ', convertDateToHinduDate($date);
?>
Output of this code:
Date: 28-01-2012 (is leap year: yes)
Converted Hindu date: 07-11-2068
But according to the calculator of this Java applet, it should be 05-11-2068 instead of 07-11-2068. So, there are still some conversion rules missing. Maybe you can give me some more information so that i can correct the code above.
I dont know whether it is correct approach or not but
Please go to http://calendarhome.com/converter/ site and download two javascript files astro.js and calendar.js and follow onchange events of Gregorian Date and fetch Indian Civil Date parameters.