Skip to main content

bugs - Unable to communicate with LinkObject when importing PDF


Bug introduced in 8 or earlier and persisting through 11.1




When I try to download some files from the web, I sometimes get an error like this:


links = {"http://dip21.bundestag.de/dip21/btd/18/076/1807620.pdf",

"http://epaper.das-parlament.de/epaper/ausgabe.pdf",
"http://gemeindebund.at/images/uploads/downloads/2014/Publikationen/RFGs/2010/RFG_4-2010_-_E-Government_in_Gemeinden_(PDF__2_4MB).pdf",
"http://smart-government.eu/wp-content/uploads/2013/02/PB_IRML-SmartSchool.pdf",
"http://subs.emis.de/LNI/Proceedings/Proceedings261/113.pdf",
"http://subs.emis.de/LNI/Proceedings/Proceedings261/163.pdf",
"http://subs.emis.de/LNI/Proceedings/Proceedings261/51.pdf",
"http://subs.emis.de/LNI/Proceedings/Proceedings261/P-261.pdf",
"https://www.bundes-sgk.de/system/files/documents/impulse2_nov2015_final.pdf",
"https://www.demo-online.de/system/files/demo_05_06_ansicht_pdf_klein.pdf",
"https://www.gruene-bw.de/app/uploads/2016/05/GrueneBW-Koalitionsvertrag-2016-Entwurf.pdf",

"https://www.itdz-berlin.de/dokumente/splitter/splitter_2005_1.pdf",
"https://www.normenkontrollrat.bund.de/Webs/NKR/Content/DE/Publikationen/Jahresberichte/2014-10-01_nkr_jahresbericht_2014.pdf?__blob=publicationFile&v=1",
"http://www.beamten-informationen.de/media/pdf/Beamten_Magazin_2008_04.pdf",
"http://www.dgb.de/themen/++co++541b3e54-263c-11e5-9b43-52540023ef1a",
"http://www.staedtetag-nrw.de/imperia/md/content/stnrw/internet/2_fachinformationen/2008/ag_4_geoportale_formatiert_korr_kes.pdf",
"http://www.vitako.de/Themen%20Dokumente/Vitako_aktuell_01-16.pdf"};
res = Import[#, "Plaintext"] & /@ links

LinkObject::linkd: Unable to communicate with closed link LinkObject["C:\Program Files\Wolfram Research\Mathematica\11.0\SystemFiles\Converters\Binaries\Windows-x86-64\PDF.exe",2011,5].


What does this error mean and how can I cleanly handle it (without keeping a PDF.exe in memory)?



Answer



Just for reference, this is my workaround:


importPatched[url_, elements_String] := importPatched[url, {elements}];
importPatched[url_List, elements_List] := Dataset[importPatched[#, elements] & /@ url];
importPatched[url_String, elements_List] := Module[{fn, pdftohtml, dlResult, pdfQ, s, elems, result, evData, i},
pdftohtml = "pdftohtml.exe"; (* edit path if necessary *)
fn = CreateFile[];
result = Prepend[Association[Rule[#, Missing[]] & /@ elements], Rule["Link", url]];


(* download *)
evData = EvaluationData[URLDownload[url, fn, {"StatusCode", "ContentType"}]];
If[Not[evData["Success"]], Return[Association[Prepend["Status" -> "URLDownload Error " <> StringRiffle[ToString /@ evData["Messages"], ", "], result]]]];
dlResult = evData["Result"];
If[dlResult["StatusCode"] =!= 200,
Quiet[DeleteFile[fn]];
Return[Prepend[result, "Status" -> "HTTP Error " <> dlResult["StatusCode"]]];
];

(* convert PDF to html *)

pdfQ = AnyTrue[{"application/acrobat", "application/pdf", "application/vnd.pdf", "application/x-pdf", "text/pdf", "text/x-pdf"},
StringContainsQ[dlResult["ContentType"], Verbatim[#], IgnoreCase -> True]& ];
s = If[pdfQ,
RenameFile[fn, fn <> ".pdf"];
fn = fn <> ".pdf";
evData = EvaluationData[StringJoin@ReadList["!" <> pdftohtml <> " -stdout " <> fn, Character]];
If[Not[evData["Success"]], Return[Prepend[result, "Status" -> "pdftohtml Error"]]];
evData["Result"]
, (* else *)
ReadString[fn]

];
Quiet[DeleteFile[fn]];

(* read requested elements *)
elems = Intersection[elements, ImportString[s, "Elements"]];
i = 1;
While[i <= Length[elems],
evData = EvaluationData[ImportString[s, elems[[i]]]];
If[Not[evData["Success"]], Return[Prepend[result, "Status" -> "Import Error " <> StringRiffle[ToString /@ evData["Messages"], ", "]]]];
result[elems[[i]]] = evData["Result"];

i++
];
Return[Prepend[result, "Status" -> "Success"]];
];
ans = importPatched[links, {"Title", "Hyperlinks"}]

It produces more or less reasonable results on all links above. It relies on the great pdftohtml tool. The function basically downloads the link, looks if it is a pdf (in which case the file is transformed to html), and then calls Import.


For calling pdftohtml I asked a separate question and got help quickly.


Any hints on bugs or possible improvements on this code very welcome! In particular, it would be interesting to know how to include the pdftohtml.exe in a subdirectory of a Mathematica package.


Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

list manipulation - Selecting multiple columns from a matrix?

Sample data: data = { {{2013, 1, 1}, 24.13, 167.67, 231.82}, {{2013, 1, 2}, 32.15, 170.92, 225.99}, {{2013, 1, 3}, 35.43, 172.68, 221.67}, {{2013, 1, 4}, 36.73, 173.05, 218.32}, {{2013, 1, 5}, 58.19, 165.96, 197.05}, {{2013, 1, 6}, 69.99, 163.50, 187.52}, {{2013, 1, 7}, 71.37, 154.21, 175.58}, {{2013, 1, 8}, 72.51, 149.66, 163.25}}; I want a DateListPlot with three graphs, so for a matrix formed by columns 1 and 2, one for columns 1 and 3, and 1 for columns 1 and 4. At the moment I'm using this code: data2 = Transpose[{data[[All, 1]], data[[All, 2]]}]; data3 = Transpose[{data[[All, 1]], data[[All, 3]]}]; data4 = Transpose[{data[[All, 1]], data[[All, 4]]}]; DateListPlot[{data2, data3, data4}, Joined -> True, Filling -> {3 -> {1}}] but I have a hunch that this can be done more efficiently. I don't like the Transpose s in particular. Any ideas? edit (for extra credit) What if I need to multiply the second column by 2, which in my solution is simp...

calculus and analysis - Integral of the Sinc product

Let us consider the following integral $$ B_n = \int_0^\infty \prod_{k=1,3,5,\dots}^n\frac{\sin (x/k)}{x/k}dx $$ By definition, Sinc[x] == Sin[x]/x , therefore B[n_?OddQ] := Integrate[Product[Sinc[x/k], {k, 1, n, 2}], {x, 0, Infinity}] B /@ Range[1, 13, 2] {Pi/2, Pi/2, Pi/2, Pi/2, Pi/2, Pi/2, Pi/2} OK, everything is fine. But... B[15] 467807924713440738696537864469 Pi/935615849440640907310521750000 What's going on? Answer As Eckhard wrote in comments B[n] is the n-th Borwein integral . (The letter B was not accidental :) ) This funny properties of Borwein integrals is related to the Fourier transform of Sinc function FourierTransform[Sinc[x], x, k] 1/2 Sqrt[Pi/2] (Sign[1 - k] + Sign[1 + k]) Plot[%, {k, -2, 2}, Filling -> 0] which is the box function. The result is $\pi/2$ while the sum $$ 1/3+1/5+\dots+1/n $$ B_n = \frac{\pi}{2} - \pi \bigg(\sum_{k=3,5,\ldots}^n\frac{1}{k} -1\biggr)^\frac{n-1}{2}\prod_{k=3,5,\dots}^n\frac{k}{k-1}. $$ For $n=15$ it is equal to $$ \frac{46780...